rev-parse --parseopt: detect missing opt-spec
commitf20b9c36d034fe37715d18ef67183cf5544227a7
authorØystein Walle <oystwa@gmail.com>
Fri, 2 Sep 2022 17:59:02 +0000 (2 19:59 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 8 Sep 2022 21:55:07 +0000 (8 14:55 -0700)
treedacea48c0900a4f671259193ce4dfd4909cb9cd5
parentac8035a2affdf30f2c691ad760826d955bba0507
rev-parse --parseopt: detect missing opt-spec

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