]> git.ipfire.org Git - thirdparty/git.git/commitdiff
rev-parse --parseopt: detect missing opt-spec
authorØystein Walle <oystwa@gmail.com>
Fri, 2 Sep 2022 17:59:02 +0000 (19:59 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 8 Sep 2022 21:55:07 +0000 (14:55 -0700)
After 2d893dff4c (rev-parse --parseopt: allow [*=?!] in argument hints,
2015-07-14) updated the parser, a line in parseopts's input can start
with one of the flag characters and be erroneously parsed as a opt-spec
where the short name of the option is the flag character itself and the
long name is after the end of the string. This makes Git want to
allocate SIZE_MAX bytes of memory at this line:

    o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);

Since s and sb.buf are equal the second argument is -2 (except unsigned)
and xmemdupz allocates len + 1 bytes, ie. -1 meaning SIZE_MAX.

Avoid this by checking whether a flag character was found in the zeroth
position.

Reported-by: Ingy dot Net <ingy@ingy.net>
Signed-off-by: Øystein Walle <oystwa@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rev-parse.c
t/t1502-rev-parse-parseopt.sh

index b259d8990a681d1804d434cea2f1e28193e6693f..85c271acd778dacf07d7ec6f39b60ae2b18f26a3 100644 (file)
@@ -479,6 +479,9 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
                if (!s)
                        s = help;
 
+               if (s == sb.buf)
+                       die(_("missing opt-spec before option flags"));
+
                if (s - sb.buf == 1) /* short option only */
                        o->short_name = *sb.buf;
                else if (sb.buf[1] != ',') /* long option only */
index 284fe18e7262ae9198d61a34142aa2e82696ce2a..de1d48f3ba29116d073b0a5573a2031fae7fcc1d 100755 (executable)
@@ -306,6 +306,13 @@ test_expect_success 'test --parseopt help output: "wrapped" options normal "or:"
        test_cmp expect actual
 '
 
+test_expect_success 'test --parseopt invalid opt-spec' '
+       test_write_lines x -- "=, x" >spec &&
+       echo "fatal: missing opt-spec before option flags" >expect &&
+       test_must_fail git rev-parse --parseopt -- >out <spec 2>err &&
+       test_cmp expect err
+'
+
 test_expect_success 'test --parseopt help output: multi-line blurb after empty line' '
        sed -e "s/^|//" >spec <<-\EOF &&
        |cmd [--some-option]