patch: fix matching failure
authorAaro Koskinen <aaro.koskinen@iki.fi>
Mon, 21 Nov 2016 22:19:51 +0000 (00:19 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Thu, 24 Nov 2016 10:40:36 +0000 (11:40 +0100)
Fix matching failure when plist is advanced while checking for buffered
lines - the lines in the hunk that are about to be added should be
skipped when checking for matching context.

Also add a valid test case that fails with current busybox and is fixed
by the change.

Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
editors/patch.c
testsuite/patch.tests

index 4ee9339a7ee762d760eb9f507712a3f3758103ee..ea1fc097497dbab39a19b78b3797f706bf00ce98 100644 (file)
@@ -290,6 +290,17 @@ static int apply_one_hunk(void)
                // out of buffer.
 
                for (;;) {
+                       while (plist && *plist->data == "+-"[reverse]) {
+                               if (!strcmp(check->data, plist->data+1) &&
+                                   !backwarn) {
+                                       backwarn = TT.linenum;
+                                       if (option_mask32 & FLAG_IGNORE) {
+                                               dummy_revert = 1;
+                                               reverse ^= 1;
+                                       }
+                               }
+                               plist = plist->next;
+                       }
                        if (!plist || strcmp(check->data, plist->data+1)) {
                                // Match failed.  Write out first line of buffered data and
                                // recheck remaining buffered data for a new match.
index 2759d2ad4b788cad47a5afb78c28ae2ba257222a..39205242c3d492a5ef6527e4426837ac8328a147 100755 (executable)
@@ -242,6 +242,51 @@ patch: can't open 'dir2///file': No such file or directory
  zxc
 "
 
+testing "patch internal buffering bug?" \
+       'patch -p1 2>&1; echo $?; cat input' \
+"\
+patching file input
+0
+foo
+
+
+
+
+
+
+1
+2
+3
+
+bar
+" \
+"\
+foo
+
+
+
+
+
+
+
+bar
+" \
+"\
+--- a/input.orig
++++ b/input
+@@ -5,5 +5,8 @@ foo
++1
++2
++3
+ bar
+-- 
+2.9.2
+" \
+
 rm input.orig 2>/dev/null
 
 exit $FAILCOUNT