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
This commit is contained in:
Junio C Hamano
2025-02-10 10:18:30 -08:00
2 changed files with 16 additions and 0 deletions

View File

@ -1423,7 +1423,10 @@ static int parse_num(const char *line, unsigned long *p)
if (!isdigit(*line))
return 0;
errno = 0;
*p = strtoul(line, &ptr, 10);
if (errno)
return 0;
return ptr - line;
}

View File

@ -38,4 +38,17 @@ incomplete (1)
incomplete (2)
EOF
test_expect_success 'applying a hunk header which overflows fails' '
cat >patch <<-\EOF &&
diff -u a/file b/file
--- a/file
+++ b/file
@@ -98765432109876543210 +98765432109876543210 @@
-a
+b
EOF
test_must_fail git apply patch 2>err &&
echo "error: corrupt patch at line 4" >expect &&
test_cmp expect err
'
test_done