From a8b8097106417d8bc337845384deadc76d312c7d Mon Sep 17 00:00:00 2001 From: Nathan Scott Date: Wed, 14 Feb 2024 10:03:51 +1100 Subject: [PATCH] Fix a compilation warning in command.c relating to kpos shadowing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- command.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/command.c b/command.c index 442cfce..30fc7ca 100644 --- a/command.c +++ b/command.c @@ -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 * @@ -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; } } @@ -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)) { @@ -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; } }