Commit 84a4f202 authored by antirez's avatar antirez
Browse files

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.
parent 5aa25250
...@@ -1299,13 +1299,15 @@ void clusterProcessGossipSection(clusterMsg *hdr, clusterLink *link) { ...@@ -1299,13 +1299,15 @@ void clusterProcessGossipSection(clusterMsg *hdr, clusterLink *link) {
clusterNode *node; clusterNode *node;
sds ci; sds ci;
ci = representClusterNodeFlags(sdsempty(), flags); if (server.verbosity == LL_DEBUG) {
serverLog(LL_DEBUG,"GOSSIP %.40s %s:%d %s", ci = representClusterNodeFlags(sdsempty(), flags);
g->nodename, serverLog(LL_DEBUG,"GOSSIP %.40s %s:%d %s",
g->ip, g->nodename,
ntohs(g->port), g->ip,
ci); ntohs(g->port),
sdsfree(ci); ci);
sdsfree(ci);
}
/* Update our state accordingly to the gossip sections */ /* Update our state accordingly to the gossip sections */
node = clusterLookupNode(g->nodename); node = clusterLookupNode(g->nodename);
...@@ -3674,15 +3676,14 @@ static struct redisNodeFlags redisNodeFlagsTable[] = { ...@@ -3674,15 +3676,14 @@ static struct redisNodeFlags redisNodeFlagsTable[] = {
/* Concatenate the comma separated list of node flags to the given SDS /* Concatenate the comma separated list of node flags to the given SDS
* string 'ci'. */ * string 'ci'. */
sds representClusterNodeFlags(sds ci, uint16_t flags) { sds representClusterNodeFlags(sds ci, uint16_t flags) {
if (flags == 0) { size_t orig_len = sdslen(ci);
ci = sdscat(ci,"noflags,"); int i, size = sizeof(redisNodeFlagsTable)/sizeof(struct redisNodeFlags);
} else { for (i = 0; i < size; i++) {
int i, size = sizeof(redisNodeFlagsTable)/sizeof(struct redisNodeFlags); struct redisNodeFlags *nodeflag = redisNodeFlagsTable + i;
for (i = 0; i < size; i++) { if (flags & nodeflag->flag) ci = sdscat(ci, nodeflag->name);
struct redisNodeFlags *nodeflag = redisNodeFlagsTable + i; }
if (flags & nodeflag->flag) ci = sdscat(ci, nodeflag->name); /* If no flag was added, add the "noflags" special flag. */
} if (sdslen(ci) == orig_len) ci = sdscat(ci,"noflags,");
}
sdsIncrLen(ci,-1); /* Remove trailing comma. */ sdsIncrLen(ci,-1); /* Remove trailing comma. */
return ci; return ci;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment