🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #4239

[PATCH] apply: reject a "diff --git" line at the end of the input

#4239
From: Tanga Joris
Date: Wed, 02 Dec 2026 02:18
95 lines
13728 bytes
e0195c4e19 (apply: reject an empty "diff --git" header, 2026-11-25)
reports a "diff --git" line without header lines as a corrupt patch.
But find_header() stops looking when fewer than 6 bytes are left after
the current line, and it does that before checking for "diff --git".
So a "diff --git" line with fewer than 6 bytes after it, such as one
at the end of the input, is still skipped, and the patches before it
apply without an error.

Check for "diff --git" before that size check, so this line is no
longer skipped. The size check stays in front of the "--- " and "+++ "
check, which reads the next line. Only compare lines longer than 11
bytes, so that memcmp() does not read past the end of a short last
line.

Reported-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Tanga Joris <tangajoris@gmail.com>
---
This applies on top of tj/apply-reject-corrupt-headers. Junio pointed
out this case in his review of v4:
https://lore.kernel.org/git/xmqqo6bc7red.fsf@gitster.g/

 apply.c                    |  8 ++++----
 t/t4133-apply-filenames.sh | 24 ++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/apply.c b/apply.c
index 2a0220fde0..b65a59fafb 100644
--- a/apply.c
+++ b/apply.c
@@ -1655,14 +1655,11 @@ static int find_header(struct apply_state *state,
 			return -128;
 		}
 
-		if (size < len + 6)
-			break;
-
 		/*
 		 * Git patch? It might not have a real patch, just a rename
 		 * or mode change, so we handle that specially
 		 */
-		if (!memcmp("diff --git ", line, 11)) {
+		if (len > 11 && !memcmp("diff --git ", line, 11)) {
 			int linenr = state->linenr;
 			int git_hdr_len = parse_git_diff_header(&state->root,
 								state->patch_input_file,
@@ -1680,6 +1677,9 @@ static int find_header(struct apply_state *state,
 			return offset;
 		}
 
+		if (size < len + 6)
+			break;
+
 		/* --- followed by +++ ? */
 		if (memcmp("--- ", line,  4) || memcmp("+++ ", line + len, 4))
 			continue;
diff --git a/t/t4133-apply-filenames.sh b/t/t4133-apply-filenames.sh
index 953749734b..43d67f2108 100755
--- a/t/t4133-apply-filenames.sh
+++ b/t/t4133-apply-filenames.sh
@@ -164,6 +164,30 @@ test_expect_success 'reject a "diff --git" line followed by another' '
 	test_cmp expect g
 '
 
+test_expect_success 'reject a "diff --git" line at the end of the patch' '
+	test_when_finished "rm -f g" &&
+	echo 1 >g &&
+	cat >end_of_patch.diff <<-\EOF &&
+	diff --git a/g b/g
+	index d00491f..0cfbf08 100644
+	--- a/g
+	+++ b/g
+	@@ -1 +1 @@
+	-1
+	+2
+	diff --git a/x b/x
+	EOF
+	reject_empty_git_header end_of_patch.diff 8 &&
+	test_must_fail git apply end_of_patch.diff &&
+	echo 1 >expect &&
+	test_cmp expect g
+'
+
+test_expect_success 'reject a patch with only a "diff --git" line' '
+	echo "diff --git a/x b/x" >only_header.diff &&
+	reject_empty_git_header only_header.diff 1
+'
+
 for old in /dev/null a/f
 do
 	test_expect_success "reject \"--- $old\" before \"new file mode\"" '

base-commit: 744a0c0bee0a62e05d7fe4c262340d114dd9ddfa
-- 
2.43.0
Technical Details
Message-ID: <20261202021845.184685-1-tangajoris@gmail.com>
Path: mbox-mo1.pugleaf.net!news-lon.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76835-linux-kernel-git=dewy.ru
References: <CAKrRMNp3TZbejSgiJYo81=sgHK=xZzr2bu2jut4Mq5wZbrr+3w@mail.gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lon.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76835-linux-kernel-git=dewy.ru
From: tangajoris@gmail.com (Tanga Joris)
Newsgroups: linux.kernel.git
Subject: [PATCH] apply: reject a "diff --git" line at the end of the input
Date: Wed,  2 Dec 2026 05:18:45 +0300
Organization: Usenet Network
Message-ID: <20261202021845.184685-1-tangajoris@gmail.com>
References: <CAKrRMNp3TZbejSgiJYo81=sgHK=xZzr2bu2jut4Mq5wZbrr+3w@mail.gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1867009"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:h6rvY/qBQrXD9xOmrMJH6AReDh8= sha256:TTXM6b6PA1t1hvmtcWRYQ/qXVGF+B+KzWXfpTuIjuZ0=
	sha1:tSknxul+YdSkfBZw40cYIfLkejI= sha256:IqlSKBhOdqYv6W3Q9NZSaPM882m8dyHTpvZFl6h00BQ=
List-ID: <git.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id B42E1117520; Wed,  2 Dec 2026 05:19:32 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.049
X-Spam-Status: No, score=-3.049 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
	FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id hPiZ7SCU8U8h;
	Wed,  2 Dec 2026 05:19:25 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-76835-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id A0B201174AD; Wed,  2 Dec 2026 05:19:15 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id C56D32C361; Wed,  2 Dec 2026 02:19:11 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796177950;
	b=haQF04jRu+hUQNqfDWqdCyPwx0Mjd4x09zIe4OWL5rFyXSUqpbehxn1gloyO0q5l4Jfi
	 ta6znq/BI0ibvnX609VC4azEWCTnRmN1A74hkd82d+8dRe7BlnECtOld0JXjc1Nu+dfrV
	 /7hdeRPX4a2KAP9D9R4973Wcfpu9jfhYMCxKnu2AkWg0a6b6KIJchffnJmMMy7J7yhfUK
	 DzhUMj/HCt1dTJzACQ13WoTC/1y3hCo4g7YhTiBBZFPmLGMRHzEXV6Dh8vvSqw8qPVbq8
	 1lXiZOk4R0OMxofW86d5fqLE7oJOpVyp5vARkm1/iERPd+XE7S+85blJqzA5Mp0LsRA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796177950;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
	 X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
	 Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:
	 References:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=jgG2u4gChhhR7JWpsk0yWVyZbD7qClUNgHmFi24j4wA=;
	b=g0Ji62AFQsmEPG5xf7MF5yw4rMlsyLj2CR4l7hzUFRJ4UsTd/zImXOr8OyGO6evzuzbu
	 Rq3WQCCs56DwV2S8yfwuP8dwzxXFMZDpOxFGOWvRRkjA0IqZ+auxPE8NlsbvPFd6j+0q2
	 Ctl4cx39VX9UFF1v2LH7SPMvy8Kuq1badO8qG040wujZEfpoSwKM2oKdxjxQTZpL6A3Xd
	 P0dJvrOlPGS3COU8F2243SPPIzlsXGRMQ4yLbiTl29qin6FcuF8sz8BDtbn/26baDM4bk
	 ThutYlcZIl/g5IvnDPjQnpV8BFc1crp0o5En5UULyBsTkiMiPu+DrBXP8LUtFCUihow==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=sle3CQrc;
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 9F555606A335;
	Wed,  2 Dec 2026 02:19:10 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=sle3CQrc
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.43
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796177949;
	b=iptukMI6ghiHUceq6Ri161aW7rPeiPPRVeZOfCFO+ZhgN1ZeDSLt1wKw+RMQV+LZGQrE
	 z0odTdlJ1DUGHxnGYzFIr6bONBZCs6DAlR712gJu37ixJM/8vfZ/gUVkWDBo4yelP72nW
	 TX8n17Lcc4SatSyYby1jCXj6S1q/pCV5D6NtTqr8jF2Nl6SbsdbhiSarWxjuxjTa9TN8/
	 bBOC3tUs0wX2cPR7sJo+bkxrK3V+xvpTaM75eHnZG3dzclUaK0JgBExP6B5pzd+IAxQ1K
	 v9ONT9uLJnXBAv+c/WXbSzvPODrLvqw0g7pcpnQOJZYepiFVL9jJ1Pr+LBL0Ko+jqYA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796177949;
	h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
	 X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:
	 Content-Transfer-Encoding;
	bh=jgG2u4gChhhR7JWpsk0yWVyZbD7qClUNgHmFi24j4wA=;
	b=mT7sFKdBWPQmzm5/vEEQqlkreJ+3b/YE/H48ANHlSxEnOh0bhsgulLDg7GNJ32Z5/Ahe
	 iSwdIGnry/yRul4O0CEFGbQIdlOMg6E6qz+hPbOPneAHTcihiEWIwKzVDKcXiMSX95kJx
	 5hCgc1zyh4trVmo15WDCKWG2rAkMzJ8YPKb/4v7jbcpdyobjrU9k2Vfqfg1EtPQnFjE/M
	 GineISRepwM0saslolv4Hp4h90mrBnZFQ0DHZLKoLc0M8JPJ/siOcSZ3u/jfBRQ92zJdi
	 GawYWbvM3Y8dk/YKfh+ROqbV2mf1z6kP+WDsZmJnJ9qs4K7Go6NU7kwPaKqgEM5JRAQ==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=sle3CQrc;
	arc=none smtp.remote-ip=209.85.128.43
X-Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 698AC606A322
	for <git@vger.kernel.org>; Wed,  2 Dec 2026 02:19:09 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 698AC606A322
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4a5177d78c7so128568515e9.2
        for <git@vger.kernel.org>; Tue, 01 Dec 2026 18:19:09 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20260707; t=1796177948; x=1796782748; darn=vger.kernel.org;
        h=content-transfer-encoding:mime-version:references:in-reply-to
         :message-id:date:subject:cc:to:from:from:to:cc:subject:date
         :message-id:reply-to:content-type;
        bh=jgG2u4gChhhR7JWpsk0yWVyZbD7qClUNgHmFi24j4wA=;
        b=sle3CQrcukdujU88QAoOyYcWWiDJaJAnYXhppUNGn0sKFhp1LqDgbdes6sn1dQu1AM
         ZX5iEKTD6dMvBtoRybIqxLunPMNJCf0qZSQwgR2AWiU43JV9je7VPtdkurV4WVFavTxb
         Fx7N7x8om+TejxbQjg5f4ewc7ufCVdv2TvjavIGzCTVQZPDLkoroaWabkBwNMNCHZQSW
         LVxZ6I5Ow6cvbvRSaBQLZxrO/bX4ZLuCcAChAhXFkPUW75KxayCIDhkSL4QtdQ3guIHv
         67jXNTde6y4N17s6ADmA0F3qMnzkduNqusEk8L9aVZ/GjU9jId795c4lDNYRdqrIpKPB
         HQ+w==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1796177948; x=1796782748;
        h=content-transfer-encoding:mime-version:references:in-reply-to
         :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from
         :to:cc:subject:date:message-id:reply-to:content-type;
        bh=jgG2u4gChhhR7JWpsk0yWVyZbD7qClUNgHmFi24j4wA=;
        b=MpFvj4jKzE0D3BwVzTvyTjpY7cs7QEN6RDVyNf1IAPCVtNDYgYiytdRlTO8nVva6OF
         3eEkUglCclx+KyP5iteHVVFL0miD6sSY5Ihgs8RCAc3EbEqd7w5weDJvSPC4K2LU0/WA
         DFnZZeb8MGiVZ9hmIRHH/ToUP+PnXsm7xVdCb8ythQ63/ZeQWU8NNK5zPTyDE2pgsFC1
         79tJspBUizgF+aws0DZ+RNM9Lc7TMMLJn8pULYy/Wog1zswPmETylcUCqQ3OiTYTKA4m
         mhrJA7gx0AMSIOihncTuezbmu2jC2TFL+1cbAKFL39XRCWMmjzAVarK2Gt5nYuzuLFAH
         7BDw==
X-Gm-Message-State: APuU4Z+nYG2hRei1B/VxCiLE89hR7PpFzBz63JOFsDMXJC2gTqkYUFqJ
	HZBfQfCNFUqMypA1PEnK0zttjpc7KYdyIKjteNHth9zL2zV3hx9q0bAXwCvkHHCrrG8=
X-Gm-Gg: AZFdTZdOydNMa+WT72NHGtGoh4pNN7NyYGh5SbneaJDvfgr2Y1Y8tLpI9TEbvSpsDqp
	pZ0MdbCKaFLlt9X6Crn5124FDqtvXm3Q54TJkkTsPbhYhMdQrGUhhyLpmRKrQ0iyqxoyF17hEyJ
	4ZAp5uUUB91R3xrFU7GfGe5WujO7kTCrO1HmSeSfFsUZw02O+1IBBcs8axjwTstKfrsgtB7agI8
	lh7N9NCXlbTI7hMB8Y63Prm/++yIjrzOWNRdB8iRepPrYfDOrBsHGVjVAw+duna8o/cQ50v8fKm
	FuVLiyGFXh/BcDVSyPGx1axkkF3RtKm/w/NisBdxaw7BfOOn/Wm7RuLRHa0WX9X168gVCjk198g
	Jlf7LrZS0YpshOfP17E9JTJ0S4U0Y7efD+NT4OdQeBPiuUxQCcyadQB/JnemmcwcciJmsC10G9d
	nnowOPo75yUosL2zxu8xhalIqUplmbTrmZ96ph3YMxFWesWYIZoXmNAUUfQqK+97Ic5ugqrS4+l
	Sxlnd8tCUjO9hbCmoLJchuNhDQgNSWzTwcICmaNFhcuCoqc/WG3qdguHSuUjht1qyCFsZOgoHts
	HniNbCH4IuJqpeylTQYI0tawIKidMOZkE7G3tkqMY0a8hsTkB6EJmfW6VgO11hkIE4brCo4z5TD
	ne2q2XN9llnqAk6UDcFcAh1G0rjzwEF0j5Vg=
X-Received: by 2002:a05:600c:848d:b0:4bb:694e:496c with SMTP id 5b1f17b1804b1-4bb88810b39mr815638485e9.22.1796177947587;
        Tue, 01 Dec 2026 18:19:07 -0800 (PST)
X-Received: from HP ([102.0.122.154])
        by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab69bf6804sm73896038f8f.10.2026.12.01.18.19.03
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Tue, 01 Dec 2026 18:19:06 -0800 (PST)
X-Cc: gitster@pobox.com,
	phillip.wood@dunelm.org.uk,
	siddharthasthana31@gmail.com,
	Tanga Joris <tangajoris@gmail.com>
X-Mailer: git-send-email 2.56.0.388.g09a996e1ff
X-In-Reply-To: <CAKrRMNp3TZbejSgiJYo81=sgHK=xZzr2bu2jut4Mq5wZbrr+3w@mail.gmail.com>
X-Mailing-List: git@vger.kernel.org
X-List-Post: <mailto:git@vger.kernel.org>
X-List-Help: <mailto:git+help@vger.kernel.org>
X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:git+owner@vger.kernel.org>
X-Precedence: list