summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Gruenbacher <agruen@gnu.org>2017-09-06 18:29:53 +0200
committerAndreas Gruenbacher <agruen@gnu.org>2017-09-06 18:35:44 +0200
commit00947f4111fbb388852e0626ba7bd1aef5d53cd9 (patch)
tree7954633e638a0970dd6f9a0f6897900ad8432119
parent61b6ec88572c15b84969048f978c339daa73341e (diff)
downloadpatch-00947f4111fbb388852e0626ba7bd1aef5d53cd9.tar.gz
Don't allow hunks to overlap
* src/patch.c (locate_hunk): Don't allow a hunk to overlap with the previous one. * tests/false-match: Add regression test.
-rw-r--r--src/patch.c2
-rw-r--r--tests/Makefile.am5
-rw-r--r--tests/false-match55
3 files changed, 59 insertions, 3 deletions
diff --git a/src/patch.c b/src/patch.c
index 1f353c1..bba7e0e 100644
--- a/src/patch.c
+++ b/src/patch.c
@@ -1135,7 +1135,7 @@ locate_hunk (lin fuzz)
lin prefix_fuzz = fuzz + prefix_context - context;
lin suffix_fuzz = fuzz + suffix_context - context;
lin max_where = input_lines - (pat_lines - suffix_fuzz) + 1;
- lin min_where = last_frozen_line + 1 - (prefix_context - prefix_fuzz);
+ lin min_where = last_frozen_line + 1;
lin max_pos_offset = max_where - first_guess;
lin max_neg_offset = first_guess - min_where;
lin max_offset = MAX(max_pos_offset, max_neg_offset);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 61a4ab8..9ae6916 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -31,7 +31,9 @@ TESTS = \
criss-cross \
crlf-handling \
dash-o-append \
+ deep-directories \
empty-files \
+ false-match \
fifo \
file-modes \
filename-choice \
@@ -57,8 +59,7 @@ TESTS = \
remember-reject-files \
remove-directories \
symlinks \
- unmodified-files \
- deep-directories
+ unmodified-files
XFAIL_TESTS = \
dash-o-append \
diff --git a/tests/false-match b/tests/false-match
new file mode 100644
index 0000000..f9a3296
--- /dev/null
+++ b/tests/false-match
@@ -0,0 +1,55 @@
+# Copyright (C) 2017 Free Software Foundation, Inc.
+#
+# Copying and distribution of this file, with or without modification,
+# in any medium, are permitted without royalty provided the copyright
+# notice and this notice are preserved.
+
+. $srcdir/test-lib.sh
+
+require cat
+use_local_patch
+use_tmpdir
+
+# ==============================================================
+
+# Check if patch incorrectly applies overlapping hunks: after the
+# first hunk is applied, the second hunk should not apply
+# anymore. Older versions of patch didn't recognize that and did
+# apply the second hunk on top of the first one, leading to the
+# following incorrect result, with no information about hunk 2:
+#
+# Hunk #1 succeeded at 7 (offset 6 lines).
+#
+# printf '%s\n' x x x x x x 1 2 3 a a 4 5 6
+
+cat > a.diff <<EOF
+--- a
++++ a
+@@ -1,6 +1,7 @@
+ 1
+ 2
+ 3
++a
+ 4
+ 5
+ 6
+@@ -7,6 +8,7 @@
+ 1
+ 2
+ 3
++a
+ 4
+ 5
+ 6
+EOF
+
+printf "%s\n" x x x x x x > a
+seq 1 6 >> a
+
+check 'patch < a.diff || echo status: $?' <<EOF
+patching file a
+Hunk #1 succeeded at 7 (offset 6 lines).
+Hunk #2 FAILED at 8.
+1 out of 2 hunks FAILED -- saving rejects to file a.rej
+status: 1
+EOF