summaryrefslogtreecommitdiff
path: root/builtin/apply.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2015-02-25 06:10:41 (GMT)
committerJunio C Hamano <gitster@pobox.com>2015-02-25 06:10:41 (GMT)
commit2764442ac9a66532789526a6e6aaf0f1254a3548 (patch)
treebc8a44f1ecd73efd97ca456ae58df917b1457597 /builtin/apply.c
parent254a3ebfe8d6061721629d9cba9b3d5f4f591ba6 (diff)
parent407a792ef7e3c142a47c6b09a80f9ede4e14f444 (diff)
downloadgit-2764442ac9a66532789526a6e6aaf0f1254a3548.zip
git-2764442ac9a66532789526a6e6aaf0f1254a3548.tar.gz
git-2764442ac9a66532789526a6e6aaf0f1254a3548.tar.bz2
Merge branch 'jc/apply-ws-fix-expands' into maint
"git apply --whitespace=fix" used to under-allocate the memory when the fix resulted in a longer text than the original patch. * jc/apply-ws-fix-expands: apply: count the size of postimage correctly apply: make update_pre_post_images() sanity check the given postlen apply.c: typofix
Diffstat (limited to 'builtin/apply.c')
-rw-r--r--builtin/apply.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/builtin/apply.c b/builtin/apply.c
index dfd7a34..c484b53 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -2230,6 +2230,12 @@ static void update_pre_post_images(struct image *preimage,
ctx++;
}
+ if (postlen
+ ? postlen < new - postimage->buf
+ : postimage->len < new - postimage->buf)
+ die("BUG: caller miscounted postlen: asked %d, orig = %d, used = %d",
+ (int)postlen, (int) postimage->len, (int)(new - postimage->buf));
+
/* Fix the length of the whole thing */
postimage->len = new - postimage->buf;
postimage->nr -= reduced;
@@ -2385,10 +2391,27 @@ static int match_fragment(struct image *img,
/*
* The hunk does not apply byte-by-byte, but the hash says
- * it might with whitespace fuzz. We haven't been asked to
+ * it might with whitespace fuzz. We weren't asked to
* ignore whitespace, we were asked to correct whitespace
* errors, so let's try matching after whitespace correction.
*
+ * While checking the preimage against the target, whitespace
+ * errors in both fixed, we count how large the corresponding
+ * postimage needs to be. The postimage prepared by
+ * apply_one_fragment() has whitespace errors fixed on added
+ * lines already, but the common lines were propagated as-is,
+ * which may become longer when their whitespace errors are
+ * fixed.
+ */
+
+ /* First count added lines in postimage */
+ postlen = 0;
+ for (i = 0; i < postimage->nr; i++) {
+ if (!(postimage->line[i].flag & LINE_COMMON))
+ postlen += postimage->line[i].len;
+ }
+
+ /*
* The preimage may extend beyond the end of the file,
* but in this loop we will only handle the part of the
* preimage that falls within the file.
@@ -2396,7 +2419,6 @@ static int match_fragment(struct image *img,
strbuf_init(&fixed, preimage->len + 1);
orig = preimage->buf;
target = img->buf + try;
- postlen = 0;
for (i = 0; i < preimage_limit; i++) {
size_t oldlen = preimage->line[i].len;
size_t tgtlen = img->line[try_lno + i].len;
@@ -2424,7 +2446,10 @@ static int match_fragment(struct image *img,
match = (tgtfix.len == fixed.len - fixstart &&
!memcmp(tgtfix.buf, fixed.buf + fixstart,
fixed.len - fixstart));
- postlen += tgtfix.len;
+
+ /* Add the length if this is common with the postimage */
+ if (preimage->line[i].flag & LINE_COMMON)
+ postlen += tgtfix.len;
strbuf_release(&tgtfix);
if (!match)