From 00947f4111fbb388852e0626ba7bd1aef5d53cd9 Mon Sep 17 00:00:00 2001 From: Andreas Gruenbacher Date: Wed, 6 Sep 2017 18:29:53 +0200 Subject: 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. --- src/patch.c | 2 +- tests/Makefile.am | 5 +++-- tests/false-match | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 tests/false-match 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 < a +seq 1 6 >> a + +check 'patch < a.diff || echo status: $?' <