From 1de675b3d52a369b88dc7a73f0bc359f7984ca44 Mon Sep 17 00:00:00 2001 From: Binbin Date: Fri, 23 Sep 2022 02:39:34 +0800 Subject: [PATCH] Fix CLUSTER SHARDS showing empty hostname (#11297) * Fix CLUSTER SHARDS showing empty hostname In #10290, we changed clusterNode hostname from `char*` to `sds`, and the old `node->hostname` was changed to `sdslen(node->hostname)!=0`. But in `addNodeDetailsToShardReply` it is missing. It results in the return of an empty string hostname in CLUSTER SHARDS command if it unavailable. Like this (note that we listed it as optional in the doc): ``` 9) "hostname" 10) "" ``` --- src/cluster.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 3a419f9d4..c10c8b464 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -5040,7 +5040,11 @@ void addNodeToNodeReply(client *c, clusterNode *node) { if (server.cluster_preferred_endpoint_type == CLUSTER_ENDPOINT_TYPE_IP) { addReplyBulkCString(c, node->ip); } else if (server.cluster_preferred_endpoint_type == CLUSTER_ENDPOINT_TYPE_HOSTNAME) { - addReplyBulkCString(c, sdslen(node->hostname) != 0 ? node->hostname : "?"); + if (sdslen(node->hostname) != 0) { + addReplyBulkCBuffer(c, node->hostname, sdslen(node->hostname)); + } else { + addReplyBulkCString(c, "?"); + } } else if (server.cluster_preferred_endpoint_type == CLUSTER_ENDPOINT_TYPE_UNKNOWN_ENDPOINT) { addReplyNull(c); } else { @@ -5066,7 +5070,7 @@ void addNodeToNodeReply(client *c, clusterNode *node) { && sdslen(node->hostname) != 0) { addReplyBulkCString(c, "hostname"); - addReplyBulkCString(c, node->hostname); + addReplyBulkCBuffer(c, node->hostname, sdslen(node->hostname)); length++; } setDeferredMapLen(c, deflen, length); @@ -5122,9 +5126,9 @@ void addNodeDetailsToShardReply(client *c, clusterNode *node) { addReplyBulkCString(c, getPreferredEndpoint(node)); reply_count++; - if (node->hostname) { + if (sdslen(node->hostname) != 0) { addReplyBulkCString(c, "hostname"); - addReplyBulkCString(c, node->hostname); + addReplyBulkCBuffer(c, node->hostname, sdslen(node->hostname)); reply_count++; }