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
This commit is contained in:
Matt Stancliff 2014-08-25 15:53:11 -05:00
parent 3cd36a4dd9
commit 391fc9b633

View File

@ -2853,24 +2853,30 @@ numargserr:
/* SENTINEL INFO [section] */ /* SENTINEL INFO [section] */
void sentinelInfoCommand(redisClient *c) { 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) { if (c->argc > 2) {
addReply(c,shared.syntaxerr); addReply(c,shared.syntaxerr);
return; 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"); if (sections++) info = sdscat(info,"\r\n");
sds serversection = genRedisInfoString("server"); sds serversection = genRedisInfoString("server");
info = sdscatlen(info,serversection,sdslen(serversection)); info = sdscatlen(info,serversection,sdslen(serversection));
sdsfree(serversection); sdsfree(serversection);
} }
if (!strcasecmp(section,"sentinel") || defsections) { if (defsections || allsections || !strcasecmp(section,"sentinel")) {
dictIterator *di; dictIterator *di;
dictEntry *de; dictEntry *de;
int master_id = 0; int master_id = 0;