summaryrefslogtreecommitdiff
path: root/src/patch.c
diff options
context:
space:
mode:
authorAndreas Gruenbacher <agruen@gnu.org>2015-01-20 12:00:24 +0100
committerAndreas Gruenbacher <agruen@gnu.org>2015-01-20 12:00:24 +0100
commit19285e563aa32a96672373a9bfe8a71a37bd7bfe (patch)
tree23165eccd49c9a6dfbc649fecb93bac3c986b934 /src/patch.c
parentae88d1c270df6ba685bd422f3bf2607367de7cfc (diff)
downloadpatch-19285e563aa32a96672373a9bfe8a71a37bd7bfe.tar.gz
Don't try applying hunks at offsets that can't work
* src/patch.c (locate_hunk): Start trying to apply the hunk at the minimum offset which puts the hunk in the valid range of lines. This will often still be offset 0.
Diffstat (limited to 'src/patch.c')
-rw-r--r--src/patch.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/patch.c b/src/patch.c
index adb2f25..441732e 100644
--- a/src/patch.c
+++ b/src/patch.c
@@ -33,6 +33,7 @@
#include <xalloc.h>
#include <gl_linked_list.h>
#include <gl_xlist.h>
+#include <minmax.h>
/* procedures */
@@ -1107,8 +1108,8 @@ locate_hunk (lin fuzz)
lin min_where = last_frozen_line + 1 - (prefix_context - prefix_fuzz);
lin max_pos_offset = max_where - first_guess;
lin max_neg_offset = first_guess - min_where;
- lin max_offset = (max_pos_offset < max_neg_offset
- ? max_neg_offset : max_pos_offset);
+ lin max_offset = MAX(max_pos_offset, max_neg_offset);
+ lin min_offset;
if (!pat_lines) /* null range matches always */
return first_guess;
@@ -1154,7 +1155,10 @@ locate_hunk (lin fuzz)
return 0;
}
- for (offset = 0; offset <= max_offset; offset++) {
+ min_offset = max_pos_offset < 0 ? first_guess - max_where
+ : max_neg_offset < 0 ? first_guess - min_where
+ : 0;
+ for (offset = min_offset; offset <= max_offset; offset++) {
char numbuf0[LINENUM_LENGTH_BOUND + 1];
char numbuf1[LINENUM_LENGTH_BOUND + 1];
if (offset <= max_pos_offset
@@ -1166,7 +1170,7 @@ locate_hunk (lin fuzz)
in_offset += offset;
return first_guess+offset;
}
- if (0 < offset && offset <= max_neg_offset
+ if (offset <= max_neg_offset
&& patch_match (first_guess, -offset, prefix_fuzz, suffix_fuzz)) {
if (debug & 1)
say ("Offset changing from %s to %s\n",