Skip to content

Commit 424e28f

Browse files
peffgitster
authored andcommitted
env--helper: write to opt->value in parseopt helper
We use OPT_CALLBACK_F() to call the option_parse_type() callback, passing it the address of "cmdmode" as the value to write to. But the callback doesn't look at opt->value at all, and instead writes to a global variable. This works out because that's the same global variable we happen to pass in, but it's rather confusing. Let's use the passed-in value instead. We'll also make "cmdmode" a local variable of the main function, ensuring we can't make the same mistake again. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e885a84 commit 424e28f

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

builtin/env--helper.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ static char const * const env__helper_usage[] = {
77
NULL
88
};
99

10-
static enum {
10+
enum cmdmode {
1111
ENV_HELPER_TYPE_BOOL = 1,
1212
ENV_HELPER_TYPE_ULONG
13-
} cmdmode = 0;
13+
};
1414

1515
static int option_parse_type(const struct option *opt, const char *arg,
1616
int unset)
1717
{
18+
enum cmdmode *cmdmode = opt->value;
19+
1820
if (!strcmp(arg, "bool"))
19-
cmdmode = ENV_HELPER_TYPE_BOOL;
21+
*cmdmode = ENV_HELPER_TYPE_BOOL;
2022
else if (!strcmp(arg, "ulong"))
21-
cmdmode = ENV_HELPER_TYPE_ULONG;
23+
*cmdmode = ENV_HELPER_TYPE_ULONG;
2224
else
2325
die(_("unrecognized --type argument, %s"), arg);
2426

@@ -33,6 +35,7 @@ int cmd_env__helper(int argc, const char **argv, const char *prefix)
3335
int ret;
3436
int ret_int, default_int;
3537
unsigned long ret_ulong, default_ulong;
38+
enum cmdmode cmdmode = 0;
3639
struct option opts[] = {
3740
OPT_CALLBACK_F(0, "type", &cmdmode, N_("type"),
3841
N_("value is given this type"), PARSE_OPT_NONEG,

0 commit comments

Comments
 (0)