Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a compilation warning in command.c relating to kpos shadowing #201

Merged
merged 1 commit into from
Mar 15, 2024

Conversation

natoscott
Copy link
Contributor

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.

Copy link
Collaborator

@zuiderkwast zuiderkwast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

Shadowing can cause bugs because you can mix up the two variables, but reusing the same variable multiple times in a larger scope can cause a similar mix up too. There's no warning for the latter though.

I see that each time the kpos variable is used, it is only the following lines, so the real scope is actually just these lines:

    kpos = hiarray_push(r->keys);
    if (kpos == NULL)
        goto oom;
    kpos->start = arg;
    kpos->end = arg + arglen;

We have a change here to avoid shadowing and also get rid of some duplicated code. We can wrap those lines in a function, like

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;
}

and call it like

    if (!push_keypos(r, arg, arglen))
        goto oom;

WDYT?

@natoscott
Copy link
Contributor Author

@zuiderkwast that's an excellent idea IMO.

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 Nordix#201.
Copy link
Collaborator

@bjosv bjosv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@bjosv bjosv merged commit a8b8097 into Nordix:master Mar 15, 2024
32 checks passed
@natoscott natoscott deleted the shadow branch March 15, 2024 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants