@@ -2494,18 +2494,21 @@ static int match_fragment(struct apply_state *state,
2494
2494
int match_beginning , int match_end )
2495
2495
{
2496
2496
int i ;
2497
- char * fixed_buf , * buf , * orig , * target ;
2498
- struct strbuf fixed ;
2499
- size_t fixed_len , postlen ;
2497
+ const char * orig , * target ;
2498
+ struct strbuf fixed = STRBUF_INIT ;
2499
+ size_t postlen ;
2500
2500
int preimage_limit ;
2501
+ int ret ;
2501
2502
2502
2503
if (preimage -> nr + current_lno <= img -> nr ) {
2503
2504
/*
2504
2505
* The hunk falls within the boundaries of img.
2505
2506
*/
2506
2507
preimage_limit = preimage -> nr ;
2507
- if (match_end && (preimage -> nr + current_lno != img -> nr ))
2508
- return 0 ;
2508
+ if (match_end && (preimage -> nr + current_lno != img -> nr )) {
2509
+ ret = 0 ;
2510
+ goto out ;
2511
+ }
2509
2512
} else if (state -> ws_error_action == correct_ws_error &&
2510
2513
(ws_rule & WS_BLANK_AT_EOF )) {
2511
2514
/*
@@ -2522,17 +2525,23 @@ static int match_fragment(struct apply_state *state,
2522
2525
* we are not removing blanks at the end, so we
2523
2526
* should reject the hunk at this position.
2524
2527
*/
2525
- return 0 ;
2528
+ ret = 0 ;
2529
+ goto out ;
2526
2530
}
2527
2531
2528
- if (match_beginning && current_lno )
2529
- return 0 ;
2532
+ if (match_beginning && current_lno ) {
2533
+ ret = 0 ;
2534
+ goto out ;
2535
+ }
2530
2536
2531
2537
/* Quick hash check */
2532
- for (i = 0 ; i < preimage_limit ; i ++ )
2538
+ for (i = 0 ; i < preimage_limit ; i ++ ) {
2533
2539
if ((img -> line [current_lno + i ].flag & LINE_PATCHED ) ||
2534
- (preimage -> line [i ].hash != img -> line [current_lno + i ].hash ))
2535
- return 0 ;
2540
+ (preimage -> line [i ].hash != img -> line [current_lno + i ].hash )) {
2541
+ ret = 0 ;
2542
+ goto out ;
2543
+ }
2544
+ }
2536
2545
2537
2546
if (preimage_limit == preimage -> nr ) {
2538
2547
/*
@@ -2545,8 +2554,10 @@ static int match_fragment(struct apply_state *state,
2545
2554
if ((match_end
2546
2555
? (current + preimage -> len == img -> len )
2547
2556
: (current + preimage -> len <= img -> len )) &&
2548
- !memcmp (img -> buf + current , preimage -> buf , preimage -> len ))
2549
- return 1 ;
2557
+ !memcmp (img -> buf + current , preimage -> buf , preimage -> len )) {
2558
+ ret = 1 ;
2559
+ goto out ;
2560
+ }
2550
2561
} else {
2551
2562
/*
2552
2563
* The preimage extends beyond the end of img, so
@@ -2555,7 +2566,7 @@ static int match_fragment(struct apply_state *state,
2555
2566
* There must be one non-blank context line that match
2556
2567
* a line before the end of img.
2557
2568
*/
2558
- char * buf_end ;
2569
+ const char * buf , * buf_end ;
2559
2570
2560
2571
buf = preimage -> buf ;
2561
2572
buf_end = buf ;
@@ -2565,21 +2576,27 @@ static int match_fragment(struct apply_state *state,
2565
2576
for ( ; buf < buf_end ; buf ++ )
2566
2577
if (!isspace (* buf ))
2567
2578
break ;
2568
- if (buf == buf_end )
2569
- return 0 ;
2579
+ if (buf == buf_end ) {
2580
+ ret = 0 ;
2581
+ goto out ;
2582
+ }
2570
2583
}
2571
2584
2572
2585
/*
2573
2586
* No exact match. If we are ignoring whitespace, run a line-by-line
2574
2587
* fuzzy matching. We collect all the line length information because
2575
2588
* we need it to adjust whitespace if we match.
2576
2589
*/
2577
- if (state -> ws_ignore_action == ignore_ws_change )
2578
- return line_by_line_fuzzy_match (img , preimage , postimage ,
2579
- current , current_lno , preimage_limit );
2590
+ if (state -> ws_ignore_action == ignore_ws_change ) {
2591
+ ret = line_by_line_fuzzy_match (img , preimage , postimage ,
2592
+ current , current_lno , preimage_limit );
2593
+ goto out ;
2594
+ }
2580
2595
2581
- if (state -> ws_error_action != correct_ws_error )
2582
- return 0 ;
2596
+ if (state -> ws_error_action != correct_ws_error ) {
2597
+ ret = 0 ;
2598
+ goto out ;
2599
+ }
2583
2600
2584
2601
/*
2585
2602
* The hunk does not apply byte-by-byte, but the hash says
@@ -2608,7 +2625,7 @@ static int match_fragment(struct apply_state *state,
2608
2625
* but in this loop we will only handle the part of the
2609
2626
* preimage that falls within the file.
2610
2627
*/
2611
- strbuf_init (& fixed , preimage -> len + 1 );
2628
+ strbuf_grow (& fixed , preimage -> len + 1 );
2612
2629
orig = preimage -> buf ;
2613
2630
target = img -> buf + current ;
2614
2631
for (i = 0 ; i < preimage_limit ; i ++ ) {
@@ -2644,8 +2661,10 @@ static int match_fragment(struct apply_state *state,
2644
2661
postlen += tgtfix .len ;
2645
2662
2646
2663
strbuf_release (& tgtfix );
2647
- if (!match )
2648
- goto unmatch_exit ;
2664
+ if (!match ) {
2665
+ ret = 0 ;
2666
+ goto out ;
2667
+ }
2649
2668
2650
2669
orig += oldlen ;
2651
2670
target += tgtlen ;
@@ -2666,9 +2685,13 @@ static int match_fragment(struct apply_state *state,
2666
2685
/* Try fixing the line in the preimage */
2667
2686
ws_fix_copy (& fixed , orig , oldlen , ws_rule , NULL );
2668
2687
2669
- for (j = fixstart ; j < fixed .len ; j ++ )
2670
- if (!isspace (fixed .buf [j ]))
2671
- goto unmatch_exit ;
2688
+ for (j = fixstart ; j < fixed .len ; j ++ ) {
2689
+ if (!isspace (fixed .buf [j ])) {
2690
+ ret = 0 ;
2691
+ goto out ;
2692
+ }
2693
+ }
2694
+
2672
2695
2673
2696
orig += oldlen ;
2674
2697
}
@@ -2678,16 +2701,16 @@ static int match_fragment(struct apply_state *state,
2678
2701
* has whitespace breakages unfixed, and fixing them makes the
2679
2702
* hunk match. Update the context lines in the postimage.
2680
2703
*/
2681
- fixed_buf = strbuf_detach (& fixed , & fixed_len );
2682
2704
if (postlen < postimage -> len )
2683
2705
postlen = 0 ;
2684
2706
update_pre_post_images (preimage , postimage ,
2685
- fixed_buf , fixed_len , postlen );
2686
- return 1 ;
2707
+ fixed .buf , fixed .len , postlen );
2687
2708
2688
- unmatch_exit :
2709
+ ret = 1 ;
2710
+
2711
+ out :
2689
2712
strbuf_release (& fixed );
2690
- return 0 ;
2713
+ return ret ;
2691
2714
}
2692
2715
2693
2716
static int find_pos (struct apply_state * state ,
0 commit comments