Git fork

apply: detect overflow when parsing hunk header

"git apply" uses strtoul() to parse the numbers in the hunk header but
silently ignores overflows. As LONG_MAX is a legitimate return value for
strtoul() we need to set errno to zero before the call to strtoul() and
check that it is still zero afterwards. The error message we display is
not particularly helpful as it does not say what was wrong. However, it
seems pretty unlikely that users are going to trigger this error in
practice and we can always improve it later if needed.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Phillip Wood and committed by
Junio C Hamano
a206058f 2f323bb1

+16
+3
apply.c
··· 1426 1427 if (!isdigit(*line)) 1428 return 0; 1429 *p = strtoul(line, &ptr, 10); 1430 return ptr - line; 1431 } 1432
··· 1426 1427 if (!isdigit(*line)) 1428 return 0; 1429 + errno = 0; 1430 *p = strtoul(line, &ptr, 10); 1431 + if (errno) 1432 + return 0; 1433 return ptr - line; 1434 } 1435
+13
t/t4100-apply-stat.sh
··· 39 incomplete (2) 40 EOF 41 42 test_done
··· 39 incomplete (2) 40 EOF 41 42 + test_expect_success 'applying a hunk header which overflows fails' ' 43 + cat >patch <<-\EOF && 44 + diff -u a/file b/file 45 + --- a/file 46 + +++ b/file 47 + @@ -98765432109876543210 +98765432109876543210 @@ 48 + -a 49 + +b 50 + EOF 51 + test_must_fail git apply patch 2>err && 52 + echo "error: corrupt patch at line 4" >expect && 53 + test_cmp expect err 54 + ' 55 test_done