@@ -137,6 +137,7 @@ void clear_apply_state(struct apply_state *state)
137137 strset_clear (& state -> removed_symlinks );
138138 strset_clear (& state -> kept_symlinks );
139139 strbuf_release (& state -> root );
140+ FREE_AND_NULL (state -> fake_ancestor );
140141
141142 /* &state->fn_table is cleared at the end of apply_patch() */
142143}
@@ -2495,18 +2496,21 @@ static int match_fragment(struct apply_state *state,
24952496 int match_beginning , int match_end )
24962497{
24972498 int i ;
2498- char * fixed_buf , * buf , * orig , * target ;
2499- struct strbuf fixed ;
2500- size_t fixed_len , postlen ;
2499+ const char * orig , * target ;
2500+ struct strbuf fixed = STRBUF_INIT ;
2501+ size_t postlen ;
25012502 int preimage_limit ;
2503+ int ret ;
25022504
25032505 if (preimage -> nr + current_lno <= img -> nr ) {
25042506 /*
25052507 * The hunk falls within the boundaries of img.
25062508 */
25072509 preimage_limit = preimage -> nr ;
2508- if (match_end && (preimage -> nr + current_lno != img -> nr ))
2509- return 0 ;
2510+ if (match_end && (preimage -> nr + current_lno != img -> nr )) {
2511+ ret = 0 ;
2512+ goto out ;
2513+ }
25102514 } else if (state -> ws_error_action == correct_ws_error &&
25112515 (ws_rule & WS_BLANK_AT_EOF )) {
25122516 /*
@@ -2523,17 +2527,23 @@ static int match_fragment(struct apply_state *state,
25232527 * we are not removing blanks at the end, so we
25242528 * should reject the hunk at this position.
25252529 */
2526- return 0 ;
2530+ ret = 0 ;
2531+ goto out ;
25272532 }
25282533
2529- if (match_beginning && current_lno )
2530- return 0 ;
2534+ if (match_beginning && current_lno ) {
2535+ ret = 0 ;
2536+ goto out ;
2537+ }
25312538
25322539 /* Quick hash check */
2533- for (i = 0 ; i < preimage_limit ; i ++ )
2540+ for (i = 0 ; i < preimage_limit ; i ++ ) {
25342541 if ((img -> line [current_lno + i ].flag & LINE_PATCHED ) ||
2535- (preimage -> line [i ].hash != img -> line [current_lno + i ].hash ))
2536- return 0 ;
2542+ (preimage -> line [i ].hash != img -> line [current_lno + i ].hash )) {
2543+ ret = 0 ;
2544+ goto out ;
2545+ }
2546+ }
25372547
25382548 if (preimage_limit == preimage -> nr ) {
25392549 /*
@@ -2546,8 +2556,10 @@ static int match_fragment(struct apply_state *state,
25462556 if ((match_end
25472557 ? (current + preimage -> len == img -> len )
25482558 : (current + preimage -> len <= img -> len )) &&
2549- !memcmp (img -> buf + current , preimage -> buf , preimage -> len ))
2550- return 1 ;
2559+ !memcmp (img -> buf + current , preimage -> buf , preimage -> len )) {
2560+ ret = 1 ;
2561+ goto out ;
2562+ }
25512563 } else {
25522564 /*
25532565 * The preimage extends beyond the end of img, so
@@ -2556,7 +2568,7 @@ static int match_fragment(struct apply_state *state,
25562568 * There must be one non-blank context line that match
25572569 * a line before the end of img.
25582570 */
2559- char * buf_end ;
2571+ const char * buf , * buf_end ;
25602572
25612573 buf = preimage -> buf ;
25622574 buf_end = buf ;
@@ -2566,21 +2578,27 @@ static int match_fragment(struct apply_state *state,
25662578 for ( ; buf < buf_end ; buf ++ )
25672579 if (!isspace (* buf ))
25682580 break ;
2569- if (buf == buf_end )
2570- return 0 ;
2581+ if (buf == buf_end ) {
2582+ ret = 0 ;
2583+ goto out ;
2584+ }
25712585 }
25722586
25732587 /*
25742588 * No exact match. If we are ignoring whitespace, run a line-by-line
25752589 * fuzzy matching. We collect all the line length information because
25762590 * we need it to adjust whitespace if we match.
25772591 */
2578- if (state -> ws_ignore_action == ignore_ws_change )
2579- return line_by_line_fuzzy_match (img , preimage , postimage ,
2580- current , current_lno , preimage_limit );
2592+ if (state -> ws_ignore_action == ignore_ws_change ) {
2593+ ret = line_by_line_fuzzy_match (img , preimage , postimage ,
2594+ current , current_lno , preimage_limit );
2595+ goto out ;
2596+ }
25812597
2582- if (state -> ws_error_action != correct_ws_error )
2583- return 0 ;
2598+ if (state -> ws_error_action != correct_ws_error ) {
2599+ ret = 0 ;
2600+ goto out ;
2601+ }
25842602
25852603 /*
25862604 * The hunk does not apply byte-by-byte, but the hash says
@@ -2609,7 +2627,7 @@ static int match_fragment(struct apply_state *state,
26092627 * but in this loop we will only handle the part of the
26102628 * preimage that falls within the file.
26112629 */
2612- strbuf_init (& fixed , preimage -> len + 1 );
2630+ strbuf_grow (& fixed , preimage -> len + 1 );
26132631 orig = preimage -> buf ;
26142632 target = img -> buf + current ;
26152633 for (i = 0 ; i < preimage_limit ; i ++ ) {
@@ -2645,8 +2663,10 @@ static int match_fragment(struct apply_state *state,
26452663 postlen += tgtfix .len ;
26462664
26472665 strbuf_release (& tgtfix );
2648- if (!match )
2649- goto unmatch_exit ;
2666+ if (!match ) {
2667+ ret = 0 ;
2668+ goto out ;
2669+ }
26502670
26512671 orig += oldlen ;
26522672 target += tgtlen ;
@@ -2667,9 +2687,13 @@ static int match_fragment(struct apply_state *state,
26672687 /* Try fixing the line in the preimage */
26682688 ws_fix_copy (& fixed , orig , oldlen , ws_rule , NULL );
26692689
2670- for (j = fixstart ; j < fixed .len ; j ++ )
2671- if (!isspace (fixed .buf [j ]))
2672- goto unmatch_exit ;
2690+ for (j = fixstart ; j < fixed .len ; j ++ ) {
2691+ if (!isspace (fixed .buf [j ])) {
2692+ ret = 0 ;
2693+ goto out ;
2694+ }
2695+ }
2696+
26732697
26742698 orig += oldlen ;
26752699 }
@@ -2679,16 +2703,16 @@ static int match_fragment(struct apply_state *state,
26792703 * has whitespace breakages unfixed, and fixing them makes the
26802704 * hunk match. Update the context lines in the postimage.
26812705 */
2682- fixed_buf = strbuf_detach (& fixed , & fixed_len );
26832706 if (postlen < postimage -> len )
26842707 postlen = 0 ;
26852708 update_pre_post_images (preimage , postimage ,
2686- fixed_buf , fixed_len , postlen );
2687- return 1 ;
2709+ fixed .buf , fixed .len , postlen );
26882710
2689- unmatch_exit :
2711+ ret = 1 ;
2712+
2713+ out :
26902714 strbuf_release (& fixed );
2691- return 0 ;
2715+ return ret ;
26922716}
26932717
26942718static int find_pos (struct apply_state * state ,
0 commit comments