@@ -2095,7 +2095,7 @@ static void update_pre_post_images(struct image *preimage,
2095
2095
char * buf ,
2096
2096
size_t len , size_t postlen )
2097
2097
{
2098
- int i , ctx ;
2098
+ int i , ctx , reduced ;
2099
2099
char * new , * old , * fixed ;
2100
2100
struct image fixed_preimage ;
2101
2101
@@ -2105,8 +2105,10 @@ static void update_pre_post_images(struct image *preimage,
2105
2105
* free "oldlines".
2106
2106
*/
2107
2107
prepare_image (& fixed_preimage , buf , len , 1 );
2108
- assert (fixed_preimage .nr == preimage -> nr );
2109
- for (i = 0 ; i < preimage -> nr ; i ++ )
2108
+ assert (postlen
2109
+ ? fixed_preimage .nr == preimage -> nr
2110
+ : fixed_preimage .nr <= preimage -> nr );
2111
+ for (i = 0 ; i < fixed_preimage .nr ; i ++ )
2110
2112
fixed_preimage .line [i ].flag = preimage -> line [i ].flag ;
2111
2113
free (preimage -> line_allocated );
2112
2114
* preimage = fixed_preimage ;
@@ -2126,7 +2128,8 @@ static void update_pre_post_images(struct image *preimage,
2126
2128
else
2127
2129
new = old ;
2128
2130
fixed = preimage -> buf ;
2129
- for (i = ctx = 0 ; i < postimage -> nr ; i ++ ) {
2131
+
2132
+ for (i = reduced = ctx = 0 ; i < postimage -> nr ; i ++ ) {
2130
2133
size_t len = postimage -> line [i ].len ;
2131
2134
if (!(postimage -> line [i ].flag & LINE_COMMON )) {
2132
2135
/* an added line -- no counterparts in preimage */
@@ -2145,8 +2148,15 @@ static void update_pre_post_images(struct image *preimage,
2145
2148
fixed += preimage -> line [ctx ].len ;
2146
2149
ctx ++ ;
2147
2150
}
2148
- if (preimage -> nr <= ctx )
2149
- die (_ ("oops" ));
2151
+
2152
+ /*
2153
+ * preimage is expected to run out, if the caller
2154
+ * fixed addition of trailing blank lines.
2155
+ */
2156
+ if (preimage -> nr <= ctx ) {
2157
+ reduced ++ ;
2158
+ continue ;
2159
+ }
2150
2160
2151
2161
/* and copy it in, while fixing the line length */
2152
2162
len = preimage -> line [ctx ].len ;
@@ -2159,6 +2169,7 @@ static void update_pre_post_images(struct image *preimage,
2159
2169
2160
2170
/* Fix the length of the whole thing */
2161
2171
postimage -> len = new - postimage -> buf ;
2172
+ postimage -> nr -= reduced ;
2162
2173
}
2163
2174
2164
2175
static int match_fragment (struct image * img ,
0 commit comments