diff --git a/apply.c b/apply.c index 4a7b6120ac..b124678b93 100644 --- a/apply.c +++ b/apply.c @@ -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; } diff --git a/t/t4100-apply-stat.sh b/t/t4100-apply-stat.sh index 146e73d8f5..a5664f3eb3 100755 --- a/t/t4100-apply-stat.sh +++ b/t/t4100-apply-stat.sh @@ -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