From 8b49e778c859840f259eaa7df2e53b6e80c6543a Mon Sep 17 00:00:00 2001 From: op Date: Wed, 3 Aug 2022 07:30:37 +0000 Subject: [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. ok stsp@ --- usr.bin/patch/patch.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/usr.bin/patch/patch.c b/usr.bin/patch/patch.c index 00f9e3f2a59..a25cb0a7b22 100644 --- a/usr.bin/patch/patch.c +++ b/usr.bin/patch/patch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: patch.c,v 1.70 2022/08/03 07:25:44 op Exp $ */ +/* $OpenBSD: patch.c,v 1.71 2022/08/03 07:30:37 op Exp $ */ /* * patch - a program to apply diffs to original files @@ -651,6 +651,8 @@ locate_hunk(LINENUM fuzz) || diff_type == UNI_DIFF)) { say("Empty context always matches.\n"); } + if (first_guess == 0) + return 1; return (first_guess); } if (max_neg_offset >= first_guess) /* do not try lines < 0 */ -- 2.20.1