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>
Phillip Wood committed
Jan 30, 2025 at 11:08 UTC
a206058fdaab6274ae7b9bdca274011efba74e11
2 files changed
+16
apply.c
+3
@@ -1426,7 +1426,10 @@ static int parse_num(const char *line, unsigned long *p)
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
t/t4100-apply-stat.sh
+13
@@ -39,4 +39,17 @@ incomplete (1)
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