Skip to content

Commit 34736ff

Browse files
committed
Merge branch 'pw/apply-ulong-overflow-check'
"git apply" internally uses unsigned long for line numbers and uses strtoul() to parse numbers on the hunk headers. It however forgot to check parse errors. * pw/apply-ulong-overflow-check: apply: detect overflow when parsing hunk header
2 parents 442b7e0 + a206058 commit 34736ff

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
@@ -1423,7 +1423,10 @@ static int parse_num(const char *line, unsigned long *p)
14231423

14241424
if (!isdigit(*line))
14251425
return 0;
1426+
errno = 0;
14261427
*p = strtoul(line, &ptr, 10);
1428+
if (errno)
1429+
return 0;
14271430
return ptr - line;
14281431
}
14291432

t/t4100-apply-stat.sh

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

41+
test_expect_success 'applying a hunk header which overflows fails' '
42+
cat >patch <<-\EOF &&
43+
diff -u a/file b/file
44+
--- a/file
45+
+++ b/file
46+
@@ -98765432109876543210 +98765432109876543210 @@
47+
-a
48+
+b
49+
EOF
50+
test_must_fail git apply patch 2>err &&
51+
echo "error: corrupt patch at line 4" >expect &&
52+
test_cmp expect err
53+
'
4154
test_done

0 commit comments

Comments
 (0)