Skip to content

Commit

Permalink
Sentinel: Improve INFO command behavior
Browse files Browse the repository at this point in the history
Improvements:
  - Return error if asking for bad 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
  • Loading branch information
mattsta committed Aug 6, 2014
1 parent 09b619a commit f92f2d2
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/sentinel.c
Expand Up @@ -2743,24 +2743,30 @@ void sentinelCommand(redisClient *c) {

/* 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;
Expand Down Expand Up @@ -2795,6 +2801,13 @@ void sentinelInfoCommand(redisClient *c) {
dictReleaseIterator(di);
}

/* If info length is 0, then the user asked for a non-existing section. */
if (sdslen(info) == 0) {
addReply(c,shared.syntaxerr);
sdsfree(info);
return;
}

addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n",
(unsigned long)sdslen(info)));
addReplySds(c,info);
Expand Down

0 comments on commit f92f2d2

Please sign in to comment.