parse-options: check empty value in OPT_INTEGER and OPT_ABBREV

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>

Nguyễn Thái Ngọc Duy committed May 29, 2019 at 16:11 UTC f7e68a08780e91d7c2f830f33457041407172b96
2 files changed +6
parse-options-cb.c
+3
@@ -16,6 +16,9 @@ int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
16 if (!arg) {
17 v = unset ? 0 : DEFAULT_ABBREV;
18 } else {
19 + if (!*arg)
20 + return error(_("option `%s' expects a numerical value"),
21 + opt->long_name);
22 v = strtol(arg, (char **)&arg, 10);
23 if (*arg)
24 return error(_("option `%s' expects a numerical value"),
parse-options.c
+3
@@ -193,6 +193,9 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
193 }
194 if (get_arg(p, opt, flags, &arg))
195 return -1;
196 + if (!*arg)
197 + return error(_("%s expects a numerical value"),
198 + optname(opt, flags));
199 *(int *)opt->value = strtol(arg, (char **)&s, 10);
200 if (*s)
201 return error(_("%s expects a numerical value"),