From: Tanga Joris <tangajoris@gmail.com>
To: git@vger.kernel.org
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>
Subject: [Outreachy PATCH v4 0/2] apply: reject corrupt "diff --git" headers
Date: Wed, 25 Nov 2026 04:10:00 +0300 [thread overview]
Message-ID: <20261125011002.589285-1-tangajoris@gmail.com> (raw)
In-Reply-To: <20261117004348.806694-1-tangajoris@gmail.com>
"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/2 rejects a "diff --git" line with no header lines after it.
2/2 rejects a new file with a "---" line before "new file mode", and
a deleted file with a "+++" line before "deleted file mode".
Changes since v3:
- 1/2 now rejects every empty "diff --git" header right away, as
Junio suggested, instead of only when a non-git patch follows it.
- Dropped v3's 1/3. It fixed a leak that only happened when apply
skipped an empty header, which it no longer does.
- The test for a "diff --git" line followed by another one now
expects the error.
- 2/2 is unchanged.
Tanga Joris (2):
apply: reject an empty "diff --git" header
apply: reject new and deleted files that have both names
apply.c | 26 ++++++-
t/t4133-apply-filenames.sh | 135 +++++++++++++++++++++++++++++++++++++
2 files changed, 159 insertions(+), 2 deletions(-)
Range-diff against v3:
1: 8136f837a9 < -: ---------- apply: free the old def_name in parse_git_diff_header()
2: 048574a48f ! 1: 3fe5c26a25 apply: reject a patch after an empty "diff --git" line
@@ Metadata
Author: Tanga Joris <tangajoris@gmail.com>
## Commit message ##
- apply: reject a patch after an empty "diff --git" line
+ apply: reject an empty "diff --git" header
When a "diff --git" line is followed by a line that is not a git
header line, such as a blank line or an indented "new file mode"
@@ Commit message
file, it applies. But its "old mode" and "new mode" lines are
ignored.
- These are corrupt patches. "git diff" does not produce them. So
- report the empty header as an error, instead of reading the patch
- that follows it. This also rejects a "diff --git" line that is
- followed by other text and then a non-git patch, which used to apply.
- A "diff --git" line followed by another "diff --git" line is handled
- as before, because 206de27efa ignores empty headers on purpose. Only
- a patch that is read under an empty header is a problem.
+ When 206de27efa was written, Git printed the "diff --git" line before
+ running diff, so a path with nothing really changed could end up
+ with just that line. ec1fcc16af (Show original and resulting blob
+ object info in diff output, 2005-10-07) added the "index" line, and a
+ pure rename or mode change has its own header lines. So today's Git
+ does not write an empty "diff --git" header, and one in a patch means
+ the patch is corrupt. Report it as an error, instead of skipping it.
+
+ This also rejects a "diff --git" line followed directly by another
+ one, which used to be skipped, and one followed by other text and
+ then a non-git patch, which used to apply. Text between patches is
+ still ignored, unless one of its lines starts with "diff --git ".
Reported-by: Gingrspacecadet <jackrawlings2011@gmail.com>
Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
+ Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Tanga Joris <tangajoris@gmail.com>
## apply.c ##
-@@ apply.c: static int find_header(struct apply_state *state,
- struct patch *patch)
- {
- unsigned long offset, len;
-+ int empty_git_hdr_linenr = 0;
-
- patch->is_toplevel_relative = 0;
- patch->is_rename = patch->is_copy = 0;
@@ apply.c: static int find_header(struct apply_state *state,
* or mode change, so we handle that specially
*/
@@ apply.c: static int find_header(struct apply_state *state,
if (git_hdr_len < 0)
return -128;
- if (git_hdr_len <= len)
+- continue;
+ if (git_hdr_len <= len) {
-+ empty_git_hdr_linenr = linenr;
- continue;
++ error(_("corrupt patch: empty \"diff --git\" header at %s:%d"),
++ state->patch_input_file, linenr);
++ return -128;
+ }
*hdrsize = git_hdr_len;
return offset;
}
-@@ apply.c: static int find_header(struct apply_state *state,
- if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
- continue;
-
-+ /* Git does not write a patch under an empty git header */
-+ if (empty_git_hdr_linenr) {
-+ error(_("corrupt patch: empty \"diff --git\" header at %s:%d"),
-+ state->patch_input_file, empty_git_hdr_linenr);
-+ return -128;
-+ }
-+
- /* Ok, we'll consider it a patch */
- if (parse_traditional_patch(state, line, line+len, patch))
- return -128;
## t/t4133-apply-filenames.sh ##
@@ t/t4133-apply-filenames.sh: test_expect_success 'apply diff with old filename missing from headers' '
@@ t/t4133-apply-filenames.sh: test_expect_success 'apply diff with old filename mi
+ reject_empty_git_header later_line.diff 3
+'
+
- test_expect_success 'ignore an empty "diff --git" line before a git patch' '
- test_when_finished "rm -f g" &&
- echo 1 >g &&
++test_expect_success 'reject a "diff --git" line followed by another' '
++ test_when_finished "rm -f g" &&
++ echo 1 >g &&
++ cat >empty_section.diff <<-\EOF &&
++ diff --git a/g b/g
++ diff --git a/g b/g
++ index d00491f..0cfbf08 100644
++ --- a/g
++ +++ b/g
++ @@ -1 +1 @@
++ -1
++ +2
++ EOF
++ reject_empty_git_header empty_section.diff 1 &&
++ echo 1 >expect &&
++ test_cmp expect g
++'
++
+ test_done
3: b3da07db98 ! 2: 91277ef405 apply: reject new and deleted files that have both names
@@ apply.c: int parse_git_diff_header(struct strbuf *root,
}
## t/t4133-apply-filenames.sh ##
-@@ t/t4133-apply-filenames.sh: test_expect_success 'ignore an empty "diff --git" line before a git patch' '
+@@ t/t4133-apply-filenames.sh: test_expect_success 'reject a "diff --git" line followed by another' '
test_cmp expect g
'
base-commit: 77904e546de3bd940621092ecec47a88d0cede5d
--
2.43.0
next prev parent reply other threads:[~2026-11-25 1:10 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-17 15:17 Git crashed on `git apply --check` Gingrspacecadet
2026-10-24 6:10 ` Tanga Joris
[not found] ` <CALdwsC11JHUBBTt3e_RFixLYSF0VU1C+mG7bQsO0vV=a68XHNw@mail.gmail.com>
2026-10-25 22:56 ` Tanga Joris
2026-11-07 19:54 ` [Outreachy PATCH 1/2] apply: do not keep names from an unused "diff --git" line Tanga Joris
2026-11-07 19:54 ` [Outreachy PATCH 2/2] apply: reject new and deleted files that have both names Tanga Joris
2026-11-09 10:52 ` [Outreachy PATCH 1/2] apply: do not keep names from an unused "diff --git" line Phillip Wood
2026-11-09 13:13 ` Tanga Joris
2026-11-09 14:31 ` Phillip Wood
2026-11-09 20:54 ` Tanga Joris
2026-11-09 21:06 ` Junio C Hamano
2026-11-09 23:03 ` Tanga Joris
2026-11-09 19:56 ` Junio C Hamano
2026-11-14 1:20 ` [Outreachy PATCH v2 1/2] apply: reject a patch after an empty " Tanga Joris
2026-11-14 1:20 ` [Outreachy PATCH v2 2/2] apply: reject new and deleted files that have both names Tanga Joris
2026-11-15 12:47 ` Siddharth Asthana
2026-11-16 18:06 ` Tanga Joris
2026-11-15 12:39 ` [Outreachy PATCH v2 1/2] apply: reject a patch after an empty "diff --git" line Siddharth Asthana
2026-11-16 17:54 ` Tanga Joris
2026-11-17 0:43 ` [Outreachy PATCH v3 0/3] apply: reject corrupt "diff --git" headers Tanga Joris
2026-11-17 0:43 ` [Outreachy PATCH v3 1/3] apply: free the old def_name in parse_git_diff_header() Tanga Joris
2026-11-18 20:51 ` Junio C Hamano
2026-11-18 23:32 ` Tanga Joris
2026-11-19 20:28 ` Junio C Hamano
2026-11-20 0:17 ` Tanga Joris
2026-11-17 0:43 ` [Outreachy PATCH v3 2/3] apply: reject a patch after an empty "diff --git" line Tanga Joris
2026-11-17 0:43 ` [Outreachy PATCH v3 3/3] apply: reject new and deleted files that have both names Tanga Joris
2026-11-25 1:10 ` Tanga Joris [this message]
2026-11-25 1:10 ` [Outreachy PATCH v4 1/2] apply: reject an empty "diff --git" header Tanga Joris
2026-11-25 17:27 ` Junio C Hamano
2026-11-25 23:38 ` Tanga Joris
2026-11-25 1:10 ` [Outreachy PATCH v4 2/2] apply: reject new and deleted files that have both names Tanga Joris
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125011002.589285-1-tangajoris@gmail.com \
--to=tangajoris@gmail.com \
--cc=christian.couder@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jackrawlings2011@gmail.com \
--cc=kaartic.sivaraam@gmail.com \
--cc=phillip.wood@dunelm.org.uk \
--cc=siddharthasthana31@gmail.com \
--cc=usmanakinyemi202@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox