From nobody Fri Aug 29 19:24:56 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4cD7WX4cdlz663gH; Fri, 29 Aug 2025 19:24:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4cD7WX3h1xz3YJp; Fri, 29 Aug 2025 19:24:56 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1756495496; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zu2XEyJkQUuW+B39gbigWWAbJwhWggZAi3bguVIXFzI=; b=ROexear7rcVkxMgG8MzmbwcA1ki5YfWNXNvo4ZUWB5i8T6hGW4/z8165AwQkpk5ZSt407E FXXT2tjBgMtHH1YN/JjOVA+zlqpZxZvEmVBxWqEkr2biBYJshF8Fx0BdAl40AOMwihjk2j w61eQOL+n6vu+rC2fsSDf3ittdJPS59cjxvsXCB8f4XDE/ubc6kBcAbCx6pTVLAJOvlaZI 7uOXXsdBuqcfGrV3dCyPHzlUi235gWOET0FmFZ0G/AJVBKT9nn4BNQTecm3FOCVJdH/CIM XGUgitIbaPxkpasn4KKMZvx5E2iFeTQ7UNz0Rl64nAnnKBSpANsH7wj/q2Q8xA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1756495496; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zu2XEyJkQUuW+B39gbigWWAbJwhWggZAi3bguVIXFzI=; b=McCR8vt8RVMk2PK7avDRX1xve4U8HhNHLLuBefCnPjAh2bdz7RNLHuAUmQNlTcyUylyQD/ IHNB0fq3DDN0CoWD5GIRU3UhANGqqJtoEaVaeaku5vt7wiGRPLvRRtFrGLTJcbAKdO4Rhk w90XpCLbY2RRu4Sozn/Zm9bBkgoZKiijT8G8w9cn6b/lJOhBZUAfnCOg42pc4h1bgFC1wi Fl4CKKPKEVADXk7cpJQPyfmmIYAk0mEfst9S+MJTaqm65HFCckAXdGzenZwcceKQijxFVA 227vz1+SrReRf9hI/Frevt2P9dCkUZdpKs5ieVqhHKlOuDs96d3SlK29KDqOMw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1756495496; a=rsa-sha256; cv=none; b=FVIYlplTEB7i3GmK3k3ffYjtHij00KjD9S3ld8RH5gNjfoFzgv+EC8DqeZzLt7TkClKzFw gvsDVviHA14EKMSFXdvmcCman/vXxFjeFpwiyP4MMXBp1Gptk/uobXMmUqwfiPU13mgkCJ 3vzMzXdcgTS3F5KaqVoq2W1Ml1o7+P0YN3t9tnKHpzNjExWLbf5/Lmy/co/Jsn1tRvaVuz Mx7dBcil5l+y5m0rRx1xtBIOeUWRyNy1gGWxkyFmiU0Xh2m2r3c/+RkzkGr5rgn6Is/XMG EX7rm0fcKX7rdQkGDWLL9A+osIfyRwMstbAQ1xKwNOXoPUx8SfAQ46wLhnlpeg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4cD7WX33JMztTl; Fri, 29 Aug 2025 19:24:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 57TJOu05071685; Fri, 29 Aug 2025 19:24:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 57TJOu05071682; Fri, 29 Aug 2025 19:24:56 GMT (envelope-from git) Date: Fri, 29 Aug 2025 19:24:56 GMT Message-Id: <202508291924.57TJOu05071682@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: f97b6a8f84b3 - main - patch: fix pch_context() for unified diffs with no leading context List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f97b6a8f84b3ed209c2aea0958a7b889d0bf27ed Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=f97b6a8f84b3ed209c2aea0958a7b889d0bf27ed commit f97b6a8f84b3ed209c2aea0958a7b889d0bf27ed Author: Kyle Evans AuthorDate: 2025-08-29 19:24:35 +0000 Commit: Kyle Evans CommitDate: 2025-08-29 19:24:35 +0000 patch: fix pch_context() for unified diffs with no leading context When the first line of a file is a removal, we may not have any leading context. Only adjusting p_context if context > 0 means that we incorrectly believe that we have 100 lines of context when the reality is that we have none. This fixes a bug with fuzz-checking, which ends up fuzzing away the line we're trying to replace if it's the first line in the file. We use pch_context() to determine a reasonable max-fuzz. PR: 250511 Reviewed by: pfg Differential Revision: https://reviews.freebsd.org/D51837 --- usr.bin/patch/pch.c | 2 +- usr.bin/patch/tests/unified_patch_test.sh | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/usr.bin/patch/pch.c b/usr.bin/patch/pch.c index 71f73125a8cb..9bc4599026ae 100644 --- a/usr.bin/patch/pch.c +++ b/usr.bin/patch/pch.c @@ -1054,7 +1054,7 @@ hunk_done: p_end = fillnew; malformed(); } - if (ch != ' ' && context > 0) { + if (ch != ' ' && context >= 0) { if (context < p_context) p_context = context; context = -1000; diff --git a/usr.bin/patch/tests/unified_patch_test.sh b/usr.bin/patch/tests/unified_patch_test.sh index 7d4b74182c41..47c4dc5faf73 100755 --- a/usr.bin/patch/tests/unified_patch_test.sh +++ b/usr.bin/patch/tests/unified_patch_test.sh @@ -25,6 +25,26 @@ # SUCH DAMAGE. # +atf_test_case badfuzz +badfuzz_head() +{ + atf_set "descr" "Test for patch(1) erroneously fuzzing away action lines" +} +badfuzz_body() +{ + # PR 250511 demonstrates a scenario where patch(1) will happily apply a + # patch into the wrong location if we have some lines that are still + # similar in the trailing context. In the following example, it would + # actually replace the underscore before the second series of B\nC\nO + # with "Z", when the patch should have been rejected instead. + printf "A\nB\nC\nO\n_\nB\nC\nO\n" > file.orig + printf "Z\nB\nC\nO\n_\nB\nC\nO\n" > file + printf "OK\nDIFF1\nDIFF2\n\n_\nB\nC\nO\n" > file.newer + + atf_check -s not-exit:0 -o save:file.patch diff -u3 file.orig file + atf_check -s not-exit:0 -o not-empty patch file.newer file.patch +} + atf_test_case basic basic_body() { @@ -161,6 +181,7 @@ EOF atf_init_test_cases() { + atf_add_test_case badfuzz atf_add_test_case basic atf_add_test_case limited_ctx atf_add_test_case file_creation