Skip to content

Commit a206058

Browse files
phillipwoodgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2f323bb commit a206058

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

apply.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,10 @@ static int parse_num(const char *line, unsigned long *p)
14261426

14271427
if (!isdigit(*line))
14281428
return 0;
1429+
errno = 0;
14291430
*p = strtoul(line, &ptr, 10);
1431+
if (errno)
1432+
return 0;
14301433
return ptr - line;
14311434
}
14321435

t/t4100-apply-stat.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,17 @@ incomplete (1)
3939
incomplete (2)
4040
EOF
4141

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+
'
4255
test_done

0 commit comments

Comments
 (0)