Skip to content

Commit

Permalink
Fix a compilation warning in command.c relating to kpos shadowing
Browse files Browse the repository at this point in the history
When building with the gcc -Wshadow option, a warning is generated
in redis_parse_cmd() because the second and third declarations of
the same-named kpos variable share scope.  It's benign but it'd be
great to have clean compilation when using this option.

This commit implements Viktor Söderqvist's refactoring approach as
described in github PR #201.
  • Loading branch information
natoscott authored and bjosv committed Mar 15, 2024
1 parent 7312881 commit a8b8097
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions command.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ char *redis_parse_bulk(char *p, char *end, char **str, uint32_t *len) {
return p;
}

static inline int push_keypos(struct cmd *r, char *arg, uint32_t arglen) {
struct keypos *kpos = hiarray_push(r->keys);
if (kpos == NULL)
return 0;
kpos->start = arg;
kpos->end = arg + arglen;
return 1;
}

/*
* Reference: http://redis.io/topics/protocol
*
Expand Down Expand Up @@ -306,11 +315,8 @@ void redis_parse_cmd(struct cmd *r) {
/* Keyword found. Now the first key is the next arg. */
if ((p = redis_parse_bulk(p, end, &arg, &arglen)) == NULL)
goto error;
struct keypos *kpos = hiarray_push(r->keys);
if (kpos == NULL)
if (!push_keypos(r, arg, arglen))
goto oom;
kpos->start = arg;
kpos->end = arg + arglen;
goto done;
}
}
Expand Down Expand Up @@ -353,11 +359,8 @@ void redis_parse_cmd(struct cmd *r) {
goto error;
}

struct keypos *kpos = hiarray_push(r->keys);
if (kpos == NULL)
if (!push_keypos(r, arg, arglen))
goto oom;
kpos->start = arg;
kpos->end = arg + arglen;

/* Special commands where we want all keys (not only the first key). */
if (redis_argx(r) || redis_argkvx(r)) {
Expand All @@ -370,11 +373,8 @@ void redis_parse_cmd(struct cmd *r) {
goto error;
if (redis_argkvx(r) && i % 2 == 0)
continue; /* not a key */
struct keypos *kpos = hiarray_push(r->keys);
if (kpos == NULL)
if (!push_keypos(r, arg, arglen))
goto oom;
kpos->start = arg;
kpos->end = arg + arglen;
}
}

Expand Down

0 comments on commit a8b8097

Please sign in to comment.