git: 7e8afbb6d605 - main - patch: fix locate_hunk in empty files

From: Pedro F. Giffuni <pfg_at_FreeBSD.org>
Date: Sun, 03 Dec 2023 17:35:03 UTC
The branch main has been updated by pfg:

URL: https://cgit.FreeBSD.org/src/commit/?id=7e8afbb6d605006d7cb867362b225d21c4f5ae57

commit 7e8afbb6d605006d7cb867362b225d21c4f5ae57
Author:     Pedro F. Giffuni <pfg@FreeBSD.org>
AuthorDate: 2023-12-03 17:33:03 +0000
Commit:     Pedro F. Giffuni <pfg@FreeBSD.org>
CommitDate: 2023-12-03 17:33:03 +0000

    patch: fix locate_hunk in empty files
    
    if `first_guess' is zero then main() assumes that locate_hunk has failed
    and aborts the patch operation.  Instead, make sure to return 1 (the
    line number) so that the patch operation can continue.
    
    Issue originally found by Neels Hofmeyr in the regress suite of the diff
    implementation for got, where the tests assume that applying a diff with
    `patch' and then again with `patch -R' yields back the original file.
    
    Obtained from:  OpenBSD (CVS patch.c,v 1.71)
---
 usr.bin/patch/patch.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/usr.bin/patch/patch.c b/usr.bin/patch/patch.c
index ecaf799fe9b6..403189bc92b1 100644
--- a/usr.bin/patch/patch.c
+++ b/usr.bin/patch/patch.c
@@ -717,6 +717,8 @@ locate_hunk(LINENUM fuzz)
 		    || diff_type == UNI_DIFF)) {
 			say("Empty context always matches.\n");
 		}
+		if (first_guess == 0)	/* empty file */
+			return 1;
 		return (first_guess);
 	}
 	if (max_neg_offset >= first_guess)	/* do not try lines < 0 */