🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #3203

[Outreachy PATCH v3 0/3] apply: reject corrupt "diff --git" headers

#3203
From: Tanga Joris
Date: Tue, 17 Nov 2026 00:43
129 lines
16791 bytes
"git apply --check" dies on an assertion in check_preimage() for some
patches with a broken "diff --git" header. This series reports these
patches as errors instead.

1/3 fixes a leak in parse_git_diff_header().
2/3 rejects a patch that is read under an empty "diff --git" line,
like a "diff --git" line with a blank line after it.
3/3 rejects a new file with a "---" line before "new file mode", and
a deleted file with a "+++" line before "deleted file mode".

Changes since v2:
 - Added 1/3, which frees patch->def_name before setting it, as
   Siddharth suggested. It brings back the test for an empty
   "diff --git" line followed by another one, which v2 dropped.
 - 2/3 is unchanged, apart from where its tests sit in the file.
 - 3/3 names the "---" or "+++" line in the error, instead of the
   file name, which was "dev/null" for "/dev/null", as Siddharth
   suggested.
 - Fixed a broken line in the 3/3 commit message.

Tanga Joris (3):
  apply: free the old def_name in parse_git_diff_header()
  apply: reject a patch after an empty "diff --git" line
  apply: reject new and deleted files that have both names

 apply.c                    |  32 ++++++++-
 t/t4133-apply-filenames.sh | 135 +++++++++++++++++++++++++++++++++++++
 2 files changed, 166 insertions(+), 1 deletion(-)

Range-diff against v2:
-:  ---------- > 1:  8136f837a9 apply: free the old def_name in parse_git_diff_header()
1:  3b802e3053 ! 2:  048574a48f apply: reject a patch after an empty "diff --git" line
    @@ t/t4133-apply-filenames.sh: test_expect_success 'apply diff with old filename mi
     +	reject_empty_git_header later_line.diff 3
     +'
     +
    - test_done
    + test_expect_success 'ignore an empty "diff --git" line before a git patch' '
    + 	test_when_finished "rm -f g" &&
    + 	echo 1 >g &&
2:  f2d40ed931 ! 3:  b3da07db98 apply: reject new and deleted files that have both names
    @@ Commit message
         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.
    +    deleted file that has a new name too. The error names the "---" or
    +    "+++" line that came before the mode line. It does not print the
    +    name, because for "/dev/null" that would be "dev/null".
     
         Git always writes the mode line first, so patches made by Git are not
    -    affected. A patch with
    -    "+++ /dev/null" before "deleted file mode" used to apply without -R.
    -    It is now rejected too.
    +    affected. A patch with "+++ /dev/null" before "deleted file mode" used
    +    to apply without -R. It is now rejected too.
     
    +    Helped-by: Siddharth Asthana <siddharthasthana31@gmail.com>
         Signed-off-by: Tanga Joris <tangajoris@gmail.com>
     
      ## apply.c ##
    @@ apply.c: int parse_git_diff_header(struct strbuf *root,
      	}
     +	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);
    ++			error(_("git diff header has \"---\" before \"new file mode\" at %s:%d"),
    ++			      patch_input_file, patch->extension_linenr);
     +		else
    -+			error(_("git diff header for new file %s names an old file %s (line %d)"),
    -+			      patch->new_name, patch->old_name, *linenr);
    ++			error(_("git diff header has \"---\" before \"new file mode\" (line %d)"),
    ++			      patch->extension_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);
    ++			error(_("git diff header has \"+++\" before \"deleted file mode\" at %s:%d"),
    ++			      patch_input_file, patch->extension_linenr);
     +		else
    -+			error(_("git diff header for deleted file %s names a new file %s (line %d)"),
    -+			      patch->old_name, patch->new_name, *linenr);
    ++			error(_("git diff header has \"+++\" before \"deleted file mode\" (line %d)"),
    ++			      patch->extension_linenr);
     +		return -128;
     +	}
      	patch->is_toplevel_relative = 1;
    @@ apply.c: int parse_git_diff_header(struct strbuf *root,
      }
     
      ## t/t4133-apply-filenames.sh ##
    -@@ t/t4133-apply-filenames.sh: test_expect_success 'name the empty "diff --git" line in the error' '
    - 	reject_empty_git_header later_line.diff 3
    +@@ t/t4133-apply-filenames.sh: test_expect_success 'ignore an empty "diff --git" line before a git patch' '
    + 	test_cmp expect g
      '
      
     +for old in /dev/null a/f
    @@ t/t4133-apply-filenames.sh: test_expect_success 'name the empty "diff --git" lin
     +		+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
    ++		test_grep "has \"---\" before \"new file mode\" at old_before_new.diff:4" err
     +	'
     +done
     +
    @@ t/t4133-apply-filenames.sh: test_expect_success 'name the empty "diff --git" lin
     +	-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_grep "has \"+++\" before \"deleted file mode\" at new_before_delete.diff:4" 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_grep "has \"+++\" before \"deleted file mode\" at new_before_delete.diff:4" err
     +'
     +
      test_done

base-commit: a018953688f1b10bddf91bff8747068f5f4746a4
-- 
2.43.0
Technical Details
Message-ID: <20261117004348.806694-1-tangajoris@gmail.com>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-75572-linux-kernel-git=dewy.ru
References: <20261114012055.127905-1-tangajoris@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-75572-linux-kernel-git=dewy.ru
From: tangajoris@gmail.com (Tanga Joris)
Newsgroups: linux.kernel.git
Subject: [Outreachy PATCH v3 0/3] apply: reject corrupt "diff --git" headers
Date: Tue, 17 Nov 2026 03:43:45 +0300
Organization: Usenet Network
Message-ID: <20261117004348.806694-1-tangajoris@gmail.com>
References: <20261114012055.127905-1-tangajoris@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2205389"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:m71Cb9S5jU3nrePzSEWoTWvbrms= sha256:6B9vtyTWSs8YqsOeRbrF2Uo+WwLhcb4QDmFxMHT+nWo=
	sha1:T5LxeUJkqCXzDQTsC+V/l3deDCk= sha256:olKdtTy3+lIwI8HFrwZw6GyXVWpDDo5/GJnQuaHMTY0=
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 4CF7E114DE5; Tue, 17 Nov 2026 03:44:46 +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 MSR4NZGuqKzH;
	Tue, 17 Nov 2026 03:44:42 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-75572-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 CCFE6114DE3; Tue, 17 Nov 2026 03:44:30 +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 0F6812B5CA; Tue, 17 Nov 2026 00:44:28 +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=1794876267;
	b=K/mZGcPS4PQkoSz64LoadCyF6pJTpx2lb448hqZ8QNgaCxM/ifSpUn7rOf99P4g7tAFM
	 8qfY4wpkJAtxYIIlhuB26rR39WXYPSPZOkYqxjket6mOiTYtvxB3A1ZovwKhiJWvyjlKW
	 7SqpV1Q4n/nO1YL2d38UR2fzbO7Mswb3X7pDMghweV5ZD9NjYWL3mQ/1gyuycnOrMwok1
	 yNU6iqvKRQq6JoNN06MmD+T2vK7NwaNUVVflLji8IIzmPdb6mA3aak6HSXOO4Oui38JOC
	 dxll5fibHqh9jmCKH4w8J1Z622ZtA2rq6pLYsj5KXWjFU3cZSDjaazZBBTqVxiK3l7A==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794876267;
	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=gyjBiekkPv6DWq0OyFEwHtb+7MBgqKby8gnjtjgWc8g=;
	b=ezvLAqOxyvzYo65eaY8ZxiFlxDAWpXa+HEt7gu/jL0+Y7JN5keAoxl7/a6MKqC+aUSKO
	 oUJMEwFllTSlRR5/CyDwKeiTXtWZunFZFn0L7nJeh3M0F1w+n7gFPcqDcIqF37xiucINj
	 0KcNO+Zn6YmY13rGy8D6EH7alVS2arXTbXvoJiZwe2esv4N65c6onVO1WGy0Rt0sKFHuU
	 oMt9VDRdANeMSpN39Gx7UDN8lqVB9b+rjnAZto8cj5uffQqkoTwgtW4wTKT0vbGjgsM9e
	 2a3Fbe83RARLeJvH0mumOMsWMx+3p6igFOxd/k/tb8VVqEkJBbvji61VZuAJz6KDsKQ==
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=bLq9D3PJ;
	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 A90C3606A330;
	Tue, 17 Nov 2026 00:44:27 +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=bLq9D3PJ
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.47
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1794876266;
	b=pPItiIi0paOxuRUp1+znYlBBg2mFJGCLnLtbvbs4Onxg9bhfhMbqXyBXdJSM9zrKHNya
	 xXoIuuj5KUbRB+ZYrPgDPKdfZF84VcE63IxUf4ia4lGJkRHScFX2SFO9J0H80be15IUY5
	 4s31tAhs+5Q1YchE8dtANzqjgV59Evlk5rMLN3MHmYrt7z7xVKB+SYo2013Z8z0sF/Ftz
	 qJTyaWg9+bhQaISeUS/AT0mt2MDHZkFf3xGA2liN2cRaZvxLWP9sv6fg1oc8aKgL9uC6r
	 oPpWrXyntorcldfeswBquU/qTtKsrqBqGM5Or5LM+RMYcJ5D/x7EQ9HYdnCgjZDvJGg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794876266;
	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=gyjBiekkPv6DWq0OyFEwHtb+7MBgqKby8gnjtjgWc8g=;
	b=BNU5+ieRWp73RvAOVpeYRKQFfPdieeVXMmQQImZ0QX06frBbzI0o5G3vEaWb2SfDZ2VT
	 g9T9xjGQK8PlpWy+gxvZWTf9UFUY+mBFXup03ZiJm1tzDJcdzngRb9vNWYhBZQWZSvzt+
	 nUMuo9sHFirU3fgDSECJJHlgDOqmxc6VRvSVZVeG8tl+flJP/mhVTHJpf6oYLf5xYHSlJ
	 +u98b5FgB4AQlG512vG+MC/1fNwGI9whlGd3dT+F2FO341YsJVac5yVNl93otbo4kiX29
	 P5N0uZfb73crUrWGsswwxx7jeLZQ4j6avNvziyeQrOdaQddmC5JvyxcDnsZ7rdq8eSA==
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=bLq9D3PJ;
	arc=none smtp.remote-ip=209.85.221.47
X-Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47])
	(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 6028D606A31C
	for <git@vger.kernel.org>; Tue, 17 Nov 2026 00:44:26 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6028D606A31C
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-f47.google.com with SMTP id ffacd0b85a97d-4a33359c1b3so348691f8f.3
        for <git@vger.kernel.org>; Mon, 16 Nov 2026 16:44:26 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20260707; t=1794876265; x=1795481065; 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=gyjBiekkPv6DWq0OyFEwHtb+7MBgqKby8gnjtjgWc8g=;
        b=bLq9D3PJahG76rDD/m3RCw8tP5LQIBAb/kl4n9YP5ie5bcVfmDiwnTJBjXIuvdvxYl
         ST1uhf3Dk34MD7J27tjsK8+/pKRI+5J8fYICY22et2+z0+YhwP9FI56sMG/TSngI/APP
         HjSxXl3uQI0kq1Jlssi68z02AakZP+22s2nCFKXQQlToA0A3UeKy2zFx2GIS0IazLLxi
         x2hsHQTxH/XQs8OJGZ4tNqVejlXgeDjYGtZTCOegZoF8YInX82Hvpy3SmTsCCSLYZV2V
         Dqhsyc05VrUKadfooVD36+VCF5caBzsnN+mAB07ZtTR8MvyoKJaw+WTi+GSCMvZERLZG
         ssrA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1794876265; x=1795481065;
        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=gyjBiekkP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UubAn4mwa
	KkBAkO0uHgAjorD23wN6P3fTNH/CwKGFyAIpfFCNesin4I7gMGc2tH1S55wlVw2CNTtDwvye9GJ
	DX8zg2ziXaBQgKdnuB5nc7HCIfWtFIYbrTznVniltzjUfriFjxDSiNwBLJyrDDpviwkcxk46ZJz
	zhGfQlI6A5pB7DpPCxY3IkzE3FmUtVI1KTw==
X-Received: by 2002:a05:6000:46ca:b0:493:bd29:a6e3 with SMTP id ffacd0b85a97d-49c70ed913cmr17779520f8f.15.1794876264411;
        Mon, 16 Nov 2026 16:44:24 -0800 (PST)
X-Received: from HP ([102.0.122.129])
        by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4a3d39b8a37sm953876f8f.46.2026.11.16.16.44.21
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Mon, 16 Nov 2026 16:44:23 -0800 (PST)
X-Cc: siddharthasthana31@gmail.com,
	phillip.wood@dunelm.org.uk,
	gitster@pobox.com,
	jackrawlings2011@gmail.com,
	christian.couder@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: <20261114012055.127905-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