@@ -2117,10 +2117,10 @@ static void update_pre_post_images(struct image *preimage,
21172117
21182118 /*
21192119 * Adjust the common context lines in postimage. This can be
2120- * done in-place when we are just doing whitespace fixing,
2121- * which does not make the string grow , but needs a new buffer
2122- * when ignoring whitespace causes the update, since in this case
2123- * we could have e.g. tabs converted to multiple spaces.
2120+ * done in-place when we are shrinking it with whitespace
2121+ * fixing , but needs a new buffer when ignoring whitespace or
2122+ * expanding leading tabs to spaces.
2123+ *
21242124 * We trust the caller to tell us if the update can be done
21252125 * in place (postlen==0) or not.
21262126 */
@@ -2185,7 +2185,7 @@ static int match_fragment(struct image *img,
21852185 int i ;
21862186 char * fixed_buf , * buf , * orig , * target ;
21872187 struct strbuf fixed ;
2188- size_t fixed_len ;
2188+ size_t fixed_len , postlen ;
21892189 int preimage_limit ;
21902190
21912191 if (preimage -> nr + try_lno <= img -> nr ) {
@@ -2335,6 +2335,7 @@ static int match_fragment(struct image *img,
23352335 strbuf_init (& fixed , preimage -> len + 1 );
23362336 orig = preimage -> buf ;
23372337 target = img -> buf + try ;
2338+ postlen = 0 ;
23382339 for (i = 0 ; i < preimage_limit ; i ++ ) {
23392340 size_t oldlen = preimage -> line [i ].len ;
23402341 size_t tgtlen = img -> line [try_lno + i ].len ;
@@ -2362,6 +2363,7 @@ static int match_fragment(struct image *img,
23622363 match = (tgtfix .len == fixed .len - fixstart &&
23632364 !memcmp (tgtfix .buf , fixed .buf + fixstart ,
23642365 fixed .len - fixstart ));
2366+ postlen += tgtfix .len ;
23652367
23662368 strbuf_release (& tgtfix );
23672369 if (!match )
@@ -2399,8 +2401,10 @@ static int match_fragment(struct image *img,
23992401 * hunk match. Update the context lines in the postimage.
24002402 */
24012403 fixed_buf = strbuf_detach (& fixed , & fixed_len );
2404+ if (postlen < postimage -> len )
2405+ postlen = 0 ;
24022406 update_pre_post_images (preimage , postimage ,
2403- fixed_buf , fixed_len , 0 );
2407+ fixed_buf , fixed_len , postlen );
24042408 return 1 ;
24052409
24062410 unmatch_exit :
0 commit comments