🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #2619

[Outreachy PATCH 2/2] apply: reject new and deleted files that have both names

#2619
From: Tanga Joris
Date: Sat, 07 Nov 2026 19:54
108 lines
14742 bytes
Git writes "new file mode" or "deleted file mode" before the "---"
and "+++" lines. The "new file mode" line sets is_new. Then
gitdiff_oldname() leaves old_name empty for "--- /dev/null".

When the "---" line comes first, is_new is still 0. So
gitdiff_verify_name() takes the name on that line as old_name. For
"--- /dev/null" this gives "dev/null", because one leading directory
is stripped. The "new file mode" line then sets is_new. The result is
a new file that also has an old name. "git apply --check" dies on it,
on the same assertion in check_preimage(). In the same way,
"+++ /dev/null" before "deleted file mode" gives a deleted file with
a new name. "git apply -R" dies on that one.

d70e9c5c8c (apply: check git diffs for mutually exclusive header
lines, 2017-06-27) made apply report other conflicting header lines,
instead of leaving them to an assertion. Do the same here. After the
header is read, reject a new file that has an old name. Reject a
deleted file that has a new name too.

Patches made by Git are therefore not affected. A patch with
"+++ /dev/null" before "deleted file mode" used to apply without -R.
It is now rejected too.

Signed-off-by: Tanga Joris <tangajoris@gmail.com>
---
I chose to reject these headers, like d70e9c5c8c does for other
conflicting lines. Another option would be to accept "--- /dev/null"
before "new file mode" as a new file, since "/dev/null" means no file
there. I'm happy to change it if that is preferred.

 apply.c                    | 20 ++++++++++++++++++++
 t/t4133-apply-filenames.sh | 31 +++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/apply.c b/apply.c
index b56b363963..76cdacff33 100644
--- a/apply.c
+++ b/apply.c
@@ -1466,6 +1466,26 @@ int parse_git_diff_header(struct strbuf *root,
 			      *linenr);
 		return -128;
 	}
+	if (patch->is_new > 0 && patch->old_name) {
+		if (patch_input_file)
+			error(_("git diff header for new file %s names an old file %s at %s:%d"),
+			      patch->new_name, patch->old_name,
+			      patch_input_file, *linenr);
+		else
+			error(_("git diff header for new file %s names an old file %s (line %d)"),
+			      patch->new_name, patch->old_name, *linenr);
+		return -128;
+	}
+	if (patch->is_delete > 0 && patch->new_name) {
+		if (patch_input_file)
+			error(_("git diff header for deleted file %s names a new file %s at %s:%d"),
+			      patch->old_name, patch->new_name,
+			      patch_input_file, *linenr);
+		else
+			error(_("git diff header for deleted file %s names a new file %s (line %d)"),
+			      patch->old_name, patch->new_name, *linenr);
+		return -128;
+	}
 	patch->is_toplevel_relative = 1;
 	return offset;
 }
diff --git a/t/t4133-apply-filenames.sh b/t/t4133-apply-filenames.sh
index 727c36e7ea..ac9090d903 100755
--- a/t/t4133-apply-filenames.sh
+++ b/t/t4133-apply-filenames.sh
@@ -150,4 +150,35 @@ test_expect_success 'blank line after "diff --git" keeps paths relative to the t
 	test_path_is_missing f
 '
 
+for old in /dev/null a/f
+do
+	test_expect_success "reject \"--- $old\" before \"new file mode\"" '
+		cat >old_before_new.diff <<-EOF &&
+		diff --git a/f b/f
+		--- $old
+		+++ b/f
+		new file mode 100644
+		@@ -0,0 +1 @@
+		+1
+		EOF
+		test_must_fail git apply --check old_before_new.diff 2>err &&
+		test_grep "header for new file f names an old file" err
+	'
+done
+
+test_expect_success 'reject "+++ /dev/null" before "deleted file mode"' '
+	cat >new_before_delete.diff <<-\EOF &&
+	diff --git a/f b/f
+	--- a/f
+	+++ /dev/null
+	deleted file mode 100644
+	@@ -1 +0,0 @@
+	-1
+	EOF
+	test_must_fail git apply --check new_before_delete.diff 2>err &&
+	test_grep "header for deleted file f names a new file" err &&
+	test_must_fail git apply --check -R new_before_delete.diff 2>err &&
+	test_grep "header for deleted file f names a new file" err
+'
+
 test_done
-- 
2.43.0
Technical Details
Message-ID: <20261107195429.96842-2-tangajoris@gmail.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-74798-linux-kernel-git=dewy.ru
References: <CALdwsC1cMKZgRPZA+pBaJe4PPs9q9W0K9CQLmM2bgUCyysTB-w@mail.gmail.com> <20261107195429.96842-1-tangajoris@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-74798-linux-kernel-git=dewy.ru
From: tangajoris@gmail.com (Tanga Joris)
Newsgroups: linux.kernel.git
Subject: [Outreachy PATCH 2/2] apply: reject new and deleted files that have both names
Date: Sat,  7 Nov 2026 22:54:29 +0300
Organization: Usenet Network
Message-ID: <20261107195429.96842-2-tangajoris@gmail.com>
References: <CALdwsC1cMKZgRPZA+pBaJe4PPs9q9W0K9CQLmM2bgUCyysTB-w@mail.gmail.com>
 <20261107195429.96842-1-tangajoris@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="4181246"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:lz9Uctf2ker4co7U4U53Wa8/itE= sha256:6nOl9yBQ/JLeM/evIJISavUFhR66km2Y9jjl2bdG8zc=
	sha1:FD/efil3xx7cDPZTgkYs4Klcaqs= sha256:EOn54xwuByl4S5fWXHbnnO4sA7MtwOASNPBPyjDwEWw=
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 2E572113491; Sat,  7 Nov 2026 23:00:37 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.048
X-Spam-Status: No, score=-2.048 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, FORGED_GMAIL_RCVD=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,
	URIBL_BLOCKED=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 M6m_6J5Xnfeg;
	Sat,  7 Nov 2026 23:00:34 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-74798-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 ECEB311348F; Sat,  7 Nov 2026 23:00:25 +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 5EDDB4694B; Sat,  7 Nov 2026 19:55:59 +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=1794081359;
	b=RiBWsfiJVL/c4VFZ9M12CylQeeFk5f11Q0CbPOA6pnMzFZY0Q8VBX9IAgCw1rt1EcYk4
	 QiGJFLQeNLn4ucElPgtR0Z4kOLBXmffRm3zTpGv+G5t5MF5eq29xYH9Fqfi990d5Baiw+
	 F84BfjNaO802SJMxKl5+XVSyuOEQquBcXwjmqqisAVP/xxosZG7UB7kmwp/GDnG/WUoYL
	 3xSP9XCRwCpJ9e1eUlYxMfnRaU9xLWXUhh3YK9JeP7R7OP9uiMA7KOZ7JdContkvynVrM
	 dvGDTttNGiXXv1DrQS4rA22Vv8p6JJ/Rom54lR8CzuaRy/MPt2J2RqZqxLuu3c8o3fQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794081359;
	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=8l/PhSRNcA+MukN3MmIfMZfCAHp2cmBDP3qaKOswNYg=;
	b=cYCk+eUSqTbYMK+uFs7gXtTHG7SxLB9b++s683iKjJz8FL5aaW1Sio0UaBI2Tk8T6Y53
	 C+qCGcCY/L7qEW5jBHknt7wXPoL5upCtkLMvxQJWftecs6Wuaa342PEYTjAT+CzA8yOPW
	 XDSEpHQqQUgEkhGDM62kyx98sUm4rW3qNgdfBSxv66OnMfNv4JTCLKMUYoetG6zlNQm7b
	 kcWbMnUXMQJCxIZ2hKrdAJ49JxGR3vzoRNTYP/uaESMeIwtr67eyBeK0cXpY3gUALG10O
	 cJojux3+ILbB+1lmSEN+ZmsUkgVDv53PGOwZ9jP/ycQyzlm0wA3AwPZNaYvlFVM80FQ==
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=GWHHSA1l;
	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 373B8606A331;
	Sat,  7 Nov 2026 19:55:59 +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=GWHHSA1l
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.42
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1794081358;
	b=KhcQpv21/tgkNitmhbgoRNDDXyhEdLS8hClgKTX2gjUCmiquuIwLi7PfrnHpbL0BRQsQ
	 OQ0q+1IkKiGtCv2gt3Apxd96liYBjFxctqx3ojpAHap5S4mt9jlSEdDF6prBezhHFG2De
	 GmpVPTUyhMZz4M28lKwkv3K3Z5BdYwY3rw/tbQ7S4JsaDdtDrd2jLbSeTIMMczM8vgXA8
	 /NFTZsL4FQkrmDDJuRcADLhIWkZjEbefuCpKkUXL96cb8kjmUk5xDjjbhGfo0fHNtk+5I
	 cJY1iw73O9s7yTtozguaMyX0sXX+gs+0jfTdGY2+7lCNBhCaAd+JLrkHFK0Xz2m7PMw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794081358;
	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=8l/PhSRNcA+MukN3MmIfMZfCAHp2cmBDP3qaKOswNYg=;
	b=2+Ho5UDcx1YAfPWK/0FRTFYnderZeZVQNiegPriNhwr017eL2ZMOBXDBiaXXWsHYQsYM
	 i1pzYEFxgm1RqNGXoVGSdAsCLL50tkD8U05nAfK/EDcyq7+pUDRep71x+1dc3tfU0XsE9
	 s+CfHxVu4LnOL5wtSZ9PisNQph+SQ02nlAKNzV86X5NkSTolZRZWZ6JwUHu9FZY6Z7oHf
	 56uxA3dMBvVu5NgLm7+dwOytk8jR64IGBjxEMdpj+45DVamuE75VBVPyDQ5hSitRxdk5O
	 XGixaYg0qzmDYpZxZhooqm8Axi6Jm2Tc7dRwUOnpV9SbmK4Y8zcUTxI3EMHhSZsl/ug==
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=GWHHSA1l;
	arc=none smtp.remote-ip=209.85.221.42
X-Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42])
	(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 E9924606A326
	for <git@vger.kernel.org>; Sat,  7 Nov 2026 19:55:57 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E9924606A326
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-wr1-f42.google.com with SMTP id ffacd0b85a97d-490585eddfcso28498f8f.0
        for <git@vger.kernel.org>; Sat, 07 Nov 2026 11:55:57 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20260707; t=1794081356; x=1794686156; 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=8l/PhSRNcA+MukN3MmIfMZfCAHp2cmBDP3qaKOswNYg=;
        b=GWHHSA1lY5c1ENguqA/Dq0JEbcerv2fAk5L7PzL3ReHl7p8cjRFKOCUH2NuVEM3LJK
         smNrIQuhcOF9FolVhyJi0HX7wVQEklAcbmFPdU4IImtaeht3xweIxaElHsDpEHwNQLU9
         LM2FUs1Mh3uoxzoyMS28fE5bjPfiwFj1HlIna7EZOJPWP4rz4Db0q11C9WpKTkE6pju3
         0d8TvmkxAXw4QC0e5p9Npar88eVlnaeDMNeFeWCqV8Bwyi6R8uoeskgEuK/WdH/BxTDg
         N2R/oiq6F9unUg2bgcNI36aW7ioz4mv5eIMTTBVvWXa8xP9LwrKFAuWpGA9GRf3ccllC
         fdnA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1794081356; x=1794686156;
        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=8l/PhSRNcA+MukN3MmIfMZfCAHp2cmBDP3qaKOswNYg=;
        b=1Y3PS6n8o66So/AJixawIiHVClwfIiWe+SRADLyRGbPFSdSldMrxuZ6kTLFSl4jrSf
         15xQY3GxGHdN9dHgJIUuAWPFLmJi9Gb4Ulmvm9qVe0gWSHDimK8ufy8Q6u6+esH7k0bY
         4JOTZTQdVlAmjawcLgcs296ZBGZZjRpyz/f0tDs4GFyq6YEIB/fhV3EHCJ5ieOsnZqZV
         fymci0UfyuaW1VhMmUOkjXFQQNjc5qwid/xFb2R39pKRx6EiJtmNoOyO5Dov+j2mYK/T
         tgUiWGJ+q47/3wIaw4dHihzu/d9pY+Uq9HC54pdJxlrtELAyqnyTINf5RjeotFqZKVTu
         TIMw==
X-Gm-Message-State: APuU4Z9x0Rrf6i/lL9L+mjB3g0HiBinVt/Nu/cn3Qv2ol2SUWe0agSQX
	ExMqOU9euxbVia1zC0/Xkf0BMIjJW+ivorw3HuncuGAB46zU3HlZ1hjQi8Tl7Hw2/hs=
X-Gm-Gg: AYBFou0TtnL137Ah4mWZEpcTB6D5wtgVzNy5wfjIjlg2Fj5MGFX36Ir5gwB+yUvUnFp
	/4IujNwKDP21JwVPTUneAah6vXuTykuiHrgDhCLFsQrR9JdW7z4RAscXnbACIeKZVQdi2+vnD2O
	oJEBLDT+1snZSSuhJxFEzFLnWEOMwMVMTIQOGldaYHTHjqBhI1tdcYwNiR2f8/B+gdYn4HmLNgY
	/iitAoHyPPA8T6nSB+Zu5SmV0wJhh6D8BKj5w4KoQKBWeJR6rpmwGcqrVIo967sFZlkVBnE4quF
	jiwwxDrohcoQddneqvb+9bMMEmd223II791prnV+ibRpjC7BHVPkBAYmJRr5lWTzfHpcMdd32ac
	6x33cQSbdxzgBBUWQ/T+AIDVaWIU+WeMmFy7FnZOydop9D8B8OI/HCSFC4w10k3NUR3Hepogpk9
	7E/+GPoHbjNlcc4q2wmx0VY+WWiG+CwaOklwX4rWsaJLtgwmL2oNN6dQIcJTcKHQUsT5IAvl6DZ
	nl01B+CouOe/ojZR8iHsgkZmwgi83jDQChPgdAVajeeajzmq/5foKSdAmKkylXKpX4SaOltoASe
	/eCPJahej9uOvfA2Ftwfet0VCd/ro8d/GgyavSdTdLoJyvlV5YcMBDVtq6YardTjyedizwOff+L
	SS4cXb3xmg2569Z67eail8rMsaC2szEn6rQ==
X-Received: by 2002:a5d:5d06:0:b0:48e:d88:55e2 with SMTP id ffacd0b85a97d-4958c779a6dmr8101f8f.42.1794081356079;
        Sat, 07 Nov 2026 11:55:56 -0800 (PST)
X-Received: from HP ([102.0.122.16])
        by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-492d770248esm11585063f8f.42.2026.11.07.11.55.52
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sat, 07 Nov 2026 11:55:55 -0800 (PST)
X-Cc: gitster@pobox.com,
	jackrawlings2011@gmail.com,
	christian.couder@gmail.com,
	siddharthasthana31@gmail.com,
	kaartic.sivaraam@gmail.com,
	usmanakinyemi202@gmail.com,
	Tanga Joris <tangajoris@gmail.com>
X-Mailer: git-send-email 2.56.0.388.g09a996e1ff
X-In-Reply-To: <20261107195429.96842-1-tangajoris@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