Skip to content

Commit f20b9c3

Browse files
Ossegitster
authored andcommitted
rev-parse --parseopt: detect missing opt-spec
After 2d893df (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 <[email protected]> Signed-off-by: Øystein Walle <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ac8035a commit f20b9c3

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

builtin/rev-parse.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
479479
if (!s)
480480
s = help;
481481

482+
if (s == sb.buf)
483+
die(_("missing opt-spec before option flags"));
484+
482485
if (s - sb.buf == 1) /* short option only */
483486
o->short_name = *sb.buf;
484487
else if (sb.buf[1] != ',') /* long option only */

t/t1502-rev-parse-parseopt.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@ test_expect_success 'test --parseopt help output: "wrapped" options normal "or:"
306306
test_cmp expect actual
307307
'
308308

309+
test_expect_success 'test --parseopt invalid opt-spec' '
310+
test_write_lines x -- "=, x" >spec &&
311+
echo "fatal: missing opt-spec before option flags" >expect &&
312+
test_must_fail git rev-parse --parseopt -- >out <spec 2>err &&
313+
test_cmp expect err
314+
'
315+
309316
test_expect_success 'test --parseopt help output: multi-line blurb after empty line' '
310317
sed -e "s/^|//" >spec <<-\EOF &&
311318
|cmd [--some-option]

0 commit comments

Comments
 (0)