parse-options: check empty value in OPT_INTEGER and OPT_ABBREV
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Wed, 29 May 2019 09:11:16 +0000 (16:11 +0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 29 May 2019 18:04:33 +0000 (11:04 -0700)
When parsing the argument for OPT_INTEGER and OPT_ABBREV, we check if we
can parse the entire argument to a number with "if (*s)". There is one
missing check: if "arg" is empty to begin with, we fail to notice.

This could happen with long option by writing like

git diff --inter-hunk-context= blah blah

Before 16ed6c97cc (diff-parseopt: convert --inter-hunk-context,
2019-03-24), --inter-hunk-context is handled by a custom parser
opt_arg() and does detect this correctly.

This restores the bahvior for --inter-hunk-context and make sure all
other integer options are handled the same (sane) way. For OPT_ABBREV
this is new behavior. But it makes it consistent with the rest.

PS. OPT_MAGNITUDE has similar code but git_parse_ulong() does detect
empty "arg". So it's good to go.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
parse-options-cb.c
parse-options.c
index 2733393546d4d4dd9a55c5934531398f4a827ead..02293493b01a5a9e91c54da9833a583ea1c643b3 100644 (file)
@@ -16,6 +16,9 @@ int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
        if (!arg) {
                v = unset ? 0 : DEFAULT_ABBREV;
        } else {
+               if (!*arg)
+                       return error(_("option `%s' expects a numerical value"),
+                                    opt->long_name);
                v = strtol(arg, (char **)&arg, 10);
                if (*arg)
                        return error(_("option `%s' expects a numerical value"),
index cec74522e56b084fbb0c8eeec47456d0bb2f7d3e..c8ee2211969219b693b68c56f0fefa11aaf10bb0 100644 (file)
@@ -193,6 +193,9 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
                }
                if (get_arg(p, opt, flags, &arg))
                        return -1;
+               if (!*arg)
+                       return error(_("%s expects a numerical value"),
+                                    optname(opt, flags));
                *(int *)opt->value = strtol(arg, (char **)&s, 10);
                if (*s)
                        return error(_("%s expects a numerical value"),