From: Tanga Joris <tangajoris@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, phillip.wood@dunelm.org.uk,
siddharthasthana31@gmail.com, Tanga Joris <tangajoris@gmail.com>
Subject: [PATCH] apply: reject a "diff --git" line at the end of the input
Date: Wed, 2 Dec 2026 05:18:45 +0300 [thread overview]
Message-ID: <20261202021845.184685-1-tangajoris@gmail.com> (raw)
In-Reply-To: <CAKrRMNp3TZbejSgiJYo81=sgHK=xZzr2bu2jut4Mq5wZbrr+3w@mail.gmail.com>
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
next prev parent reply other threads:[~2026-12-02 2:19 UTC|newest]
Thread overview: 32+ 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 ` [Outreachy PATCH v4 0/2] apply: reject corrupt "diff --git" headers Tanga Joris
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-12-02 2:18 ` Tanga Joris [this message]
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=20261202021845.184685-1-tangajoris@gmail.com \
--to=tangajoris@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=phillip.wood@dunelm.org.uk \
--cc=siddharthasthana31@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.