@matheusbsbs / matheus-bagliano / commits / 424e28fcad
Fork 0

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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 30, 2020 at 08:28 UTC 424e28fcadfe0a40e444687c10fb4eaff8360f8d
1 file changed +7 -4
builtin/env--helper.c
+7 -4
@@ -7,18 +7,20 @@ static char const * const env__helper_usage[] = {
7 NULL
8 };
9
10 -static enum {
10 +enum cmdmode {
11 ENV_HELPER_TYPE_BOOL = 1,
12 ENV_HELPER_TYPE_ULONG
13 -} cmdmode = 0;
13 +};
14
15 static int option_parse_type(const struct option *opt, const char *arg,
16 int unset)
17 {
18 + enum cmdmode *cmdmode = opt->value;
19 +
20 if (!strcmp(arg, "bool"))
19 - cmdmode = ENV_HELPER_TYPE_BOOL;
21 + *cmdmode = ENV_HELPER_TYPE_BOOL;
22 else if (!strcmp(arg, "ulong"))
21 - cmdmode = ENV_HELPER_TYPE_ULONG;
23 + *cmdmode = ENV_HELPER_TYPE_ULONG;
24 else
25 die(_("unrecognized --type argument, %s"), arg);
26
@@ -33,6 +35,7 @@ int cmd_env__helper(int argc, const char **argv, const char *prefix)
35 int ret;
36 int ret_int, default_int;
37 unsigned long ret_ulong, default_ulong;
38 + enum cmdmode cmdmode = 0;
39 struct option opts[] = {
40 OPT_CALLBACK_F(0, "type", &cmdmode, N_("type"),
41 N_("value is given this type"), PARSE_OPT_NONEG,