From 391fc9b6335329e513664c69bdc18865ab944beb Mon Sep 17 00:00:00 2001 From: Matt Stancliff Date: Mon, 25 Aug 2014 15:53:11 -0500 Subject: [PATCH] Sentinel: Improve INFO command behavior Improvements: - Return empty string if asking for non-existing section (INFO foo) - Fix potential memory leak (caused by sdsempty() then returned if >2 args) - Clean up argument parsing - Allow "all" as valid section (same as "default" or zero args currently) - Move strcasecmp to end of evaluation chain in conditionals Also, since we're C99, I moved some variable declarations to be closer to where they are actually used (saves us from needing to free an empty info if detect argument errors up front). Closes #1915 Closes #1966 --- src/sentinel.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/sentinel.c b/src/sentinel.c index 12f15ff3..bd2d42ac 100644 --- a/src/sentinel.c +++ b/src/sentinel.c @@ -2853,24 +2853,30 @@ numargserr: /* SENTINEL INFO [section] */ void sentinelInfoCommand(redisClient *c) { - char *section = c->argc == 2 ? c->argv[1]->ptr : "default"; - sds info = sdsempty(); - int defsections = !strcasecmp(section,"default"); - int sections = 0; - if (c->argc > 2) { addReply(c,shared.syntaxerr); return; } - if (!strcasecmp(section,"server") || defsections) { + int defsections = 0, allsections = 0; + char *section = c->argc == 2 ? c->argv[1]->ptr : NULL; + if (section) { + allsections = !strcasecmp(section,"all"); + defsections = !strcasecmp(section,"default"); + } else { + defsections = 1; + } + + int sections = 0; + sds info = sdsempty(); + if (defsections || allsections || !strcasecmp(section,"server")) { if (sections++) info = sdscat(info,"\r\n"); sds serversection = genRedisInfoString("server"); info = sdscatlen(info,serversection,sdslen(serversection)); sdsfree(serversection); } - if (!strcasecmp(section,"sentinel") || defsections) { + if (defsections || allsections || !strcasecmp(section,"sentinel")) { dictIterator *di; dictEntry *de; int master_id = 0;