Skip to content

Commit b6c55a8

Browse files
committed
Make representClusterNodeFlags() more robust.
This function failed when an internal-only flag was set as an only flag in a node: the string was trimmed expecting a final comma before exiting the function, causing a crash. See issue #4142. Moreover generation of flags representation only needed at DEBUG log level was always performed: a waste of CPU time. This is fixed as well by this commit.
1 parent 9a4f3d7 commit b6c55a8

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/cluster.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,14 +1323,16 @@ void clusterProcessGossipSection(clusterMsg *hdr, clusterLink *link) {
13231323
clusterNode *node;
13241324
sds ci;
13251325

1326-
ci = representClusterNodeFlags(sdsempty(), flags);
1327-
serverLog(LL_DEBUG,"GOSSIP %.40s %s:%d@%d %s",
1328-
g->nodename,
1329-
g->ip,
1330-
ntohs(g->port),
1331-
ntohs(g->cport),
1332-
ci);
1333-
sdsfree(ci);
1326+
if (server.verbosity == LL_DEBUG) {
1327+
ci = representClusterNodeFlags(sdsempty(), flags);
1328+
serverLog(LL_DEBUG,"GOSSIP %.40s %s:%d@%d %s",
1329+
g->nodename,
1330+
g->ip,
1331+
ntohs(g->port),
1332+
ntohs(g->cport),
1333+
ci);
1334+
sdsfree(ci);
1335+
}
13341336

13351337
/* Update our state accordingly to the gossip sections */
13361338
node = clusterLookupNode(g->nodename);
@@ -3835,15 +3837,14 @@ static struct redisNodeFlags redisNodeFlagsTable[] = {
38353837
/* Concatenate the comma separated list of node flags to the given SDS
38363838
* string 'ci'. */
38373839
sds representClusterNodeFlags(sds ci, uint16_t flags) {
3838-
if (flags == 0) {
3839-
ci = sdscat(ci,"noflags,");
3840-
} else {
3841-
int i, size = sizeof(redisNodeFlagsTable)/sizeof(struct redisNodeFlags);
3842-
for (i = 0; i < size; i++) {
3843-
struct redisNodeFlags *nodeflag = redisNodeFlagsTable + i;
3844-
if (flags & nodeflag->flag) ci = sdscat(ci, nodeflag->name);
3845-
}
3846-
}
3840+
size_t orig_len = sdslen(ci);
3841+
int i, size = sizeof(redisNodeFlagsTable)/sizeof(struct redisNodeFlags);
3842+
for (i = 0; i < size; i++) {
3843+
struct redisNodeFlags *nodeflag = redisNodeFlagsTable + i;
3844+
if (flags & nodeflag->flag) ci = sdscat(ci, nodeflag->name);
3845+
}
3846+
/* If no flag was added, add the "noflags" special flag. */
3847+
if (sdslen(ci) == orig_len) ci = sdscat(ci,"noflags,");
38473848
sdsIncrLen(ci,-1); /* Remove trailing comma. */
38483849
return ci;
38493850
}

0 commit comments

Comments
 (0)