summaryrefslogtreecommitdiff
path: root/builtin/apply.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2013-01-07 06:10:23 (GMT)
committerJunio C Hamano <gitster@pobox.com>2013-01-07 06:10:23 (GMT)
commit1965f8cdbdf86930b99fd61ed2efb22ac3d6a725 (patch)
tree69bc7a0dc0ee9c83f126523c4412bebc31e33b36 /builtin/apply.c
parent32238aeb730fda0f66ccc6ec2f1e342af8b9fd04 (diff)
parent5de7166d46d25fed3b8a181c3842cf4350ecdea4 (diff)
downloadgit-1965f8cdbdf86930b99fd61ed2efb22ac3d6a725.zip
git-1965f8cdbdf86930b99fd61ed2efb22ac3d6a725.tar.gz
git-1965f8cdbdf86930b99fd61ed2efb22ac3d6a725.tar.bz2
Merge branch 'jc/apply-trailing-blank-removal'
Fix to update_pre_post_images() that did not take into account the possibility that whitespace fix could shrink the preimage and change the number of lines in it. * jc/apply-trailing-blank-removal: apply.c:update_pre_post_images(): the preimage can be truncated
Diffstat (limited to 'builtin/apply.c')
-rw-r--r--builtin/apply.c23
1 files changed, 17 insertions, 6 deletions
diff --git a/builtin/apply.c b/builtin/apply.c
index 156b3ce..6c11e8b 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -2095,7 +2095,7 @@ static void update_pre_post_images(struct image *preimage,
char *buf,
size_t len, size_t postlen)
{
- int i, ctx;
+ int i, ctx, reduced;
char *new, *old, *fixed;
struct image fixed_preimage;
@@ -2105,8 +2105,10 @@ static void update_pre_post_images(struct image *preimage,
* free "oldlines".
*/
prepare_image(&fixed_preimage, buf, len, 1);
- assert(fixed_preimage.nr == preimage->nr);
- for (i = 0; i < preimage->nr; i++)
+ assert(postlen
+ ? fixed_preimage.nr == preimage->nr
+ : fixed_preimage.nr <= preimage->nr);
+ for (i = 0; i < fixed_preimage.nr; i++)
fixed_preimage.line[i].flag = preimage->line[i].flag;
free(preimage->line_allocated);
*preimage = fixed_preimage;
@@ -2126,7 +2128,8 @@ static void update_pre_post_images(struct image *preimage,
else
new = old;
fixed = preimage->buf;
- for (i = ctx = 0; i < postimage->nr; i++) {
+
+ for (i = reduced = ctx = 0; i < postimage->nr; i++) {
size_t len = postimage->line[i].len;
if (!(postimage->line[i].flag & LINE_COMMON)) {
/* an added line -- no counterparts in preimage */
@@ -2145,8 +2148,15 @@ static void update_pre_post_images(struct image *preimage,
fixed += preimage->line[ctx].len;
ctx++;
}
- if (preimage->nr <= ctx)
- die(_("oops"));
+
+ /*
+ * preimage is expected to run out, if the caller
+ * fixed addition of trailing blank lines.
+ */
+ if (preimage->nr <= ctx) {
+ reduced++;
+ continue;
+ }
/* and copy it in, while fixing the line length */
len = preimage->line[ctx].len;
@@ -2159,6 +2169,7 @@ static void update_pre_post_images(struct image *preimage,
/* Fix the length of the whole thing */
postimage->len = new - postimage->buf;
+ postimage->nr -= reduced;
}
static int match_fragment(struct image *img,