@matheusbsbs / matheus-bagliano / commits / 52f3c81a9d
Fork 0

apply: do not barf on patch with too large an offset

Previously a patch that records too large a line number caused the offset matching code in git-apply to overstep its internal buffer. Noticed by Johannes Schindelin. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Feb 11, 2008 at 15:32 UTC 52f3c81a9d41af019ab8f05051c5251f078b12e5
2 files changed +60
builtin-apply.c
+3
@@ -1809,6 +1809,9 @@ static int find_pos(struct image *img,
1809 else if (match_end)
1810 line = img->nr - preimage->nr;
1811
1812 + if (line > img->nr)
1813 + line = img->nr;
1814 +
1815 try = 0;
1816 for (i = 0; i < line; i++)
1817 try += img->line[i].len;
t/t4105-apply-fuzz.sh new
+57
@@ -0,0 +1,57 @@
1 +#!/bin/sh
2 +
3 +test_description='apply with fuzz and offset'
4 +
5 +. ./test-lib.sh
6 +
7 +dotest () {
8 + name="$1" && shift &&
9 + test_expect_success "$name" "
10 + git checkout-index -f -q -u file &&
11 + git apply $* &&
12 + diff -u expect file
13 + "
14 +}
15 +
16 +test_expect_success setup '
17 +
18 + for i in 1 2 3 4 5 6 7 8 9 10 11 12
19 + do
20 + echo $i
21 + done >file &&
22 + git update-index --add file &&
23 + for i in 1 2 3 4 5 6 7 a b c d e 8 9 10 11 12
24 + do
25 + echo $i
26 + done >file &&
27 + cat file >expect &&
28 + git diff >O0.diff &&
29 +
30 + sed -e "s/@@ -5,6 +5,11 @@/@@ -2,6 +2,11 @@/" >O1.diff O0.diff &&
31 + sed -e "s/@@ -5,6 +5,11 @@/@@ -7,6 +7,11 @@/" >O2.diff O0.diff &&
32 + sed -e "s/@@ -5,6 +5,11 @@/@@ -19,6 +19,11 @@/" >O3.diff O0.diff &&
33 +
34 + sed -e "s/^ 5/ S/" >F0.diff O0.diff &&
35 + sed -e "s/^ 5/ S/" >F1.diff O1.diff &&
36 + sed -e "s/^ 5/ S/" >F2.diff O2.diff &&
37 + sed -e "s/^ 5/ S/" >F3.diff O3.diff
38 +
39 +'
40 +
41 +dotest 'unmodified patch' O0.diff
42 +
43 +dotest 'minus offset' O1.diff
44 +
45 +dotest 'plus offset' O2.diff
46 +
47 +dotest 'big offset' O3.diff
48 +
49 +dotest 'fuzz with no offset' -C2 F0.diff
50 +
51 +dotest 'fuzz with minus offset' -C2 F1.diff
52 +
53 +dotest 'fuzz with plus offset' -C2 F2.diff
54 +
55 +dotest 'fuzz with big offset' -C2 F3.diff
56 +
57 +test_done