* [Outreachy PATCH v2 2/2] apply: reject new and deleted files that have both names
2026-11-14 1:20 ` [Outreachy PATCH v2 1/2] apply: reject a patch after an empty " Tanga Joris
@ 2026-11-14 1:20 ` Tanga Joris
2026-11-15 12:47 ` Siddharth Asthana
2026-11-15 12:39 ` [Outreachy PATCH v2 1/2] apply: reject a patch after an empty "diff --git" line Siddharth Asthana
2026-11-17 0:43 ` [Outreachy PATCH v3 0/3] apply: reject corrupt "diff --git" headers Tanga Joris
2 siblings, 1 reply; 22+ messages in thread
From: Tanga Joris @ 2026-11-14 1:20 UTC (permalink / raw)
To: git
Cc: phillip.wood, gitster, jackrawlings2011, christian.couder,
siddharthasthana31, kaartic.sivaraam, usmanakinyemi202,
Tanga Joris
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.
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.
Signed-off-by: Tanga Joris <tangajoris@gmail.com>
---
apply.c | 20 ++++++++++++++++++++
t/t4133-apply-filenames.sh | 31 +++++++++++++++++++++++++++++++
2 files changed, 51 insertions(+)
diff --git a/apply.c b/apply.c
index c5d4fe7887..0fa6a15a65 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 751a01f92f..ff48e68da0 100755
--- a/t/t4133-apply-filenames.sh
+++ b/t/t4133-apply-filenames.sh
@@ -146,4 +146,35 @@ test_expect_success 'name the empty "diff --git" line in the error' '
reject_empty_git_header later_line.diff 3
'
+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
^ permalink raw reply related [flat|nested] 22+ messages in thread* Re: [Outreachy PATCH v2 2/2] apply: reject new and deleted files that have both names
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
0 siblings, 1 reply; 22+ messages in thread
From: Siddharth Asthana @ 2026-11-15 12:47 UTC (permalink / raw)
To: Tanga Joris, git
Cc: phillip.wood, gitster, jackrawlings2011, christian.couder,
kaartic.sivaraam, usmanakinyemi202
On 14/11/26 06:50, Tanga Joris wrote:
> 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.
>
> 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.
>
> Signed-off-by: Tanga Joris <tangajoris@gmail.com>
> ---
> apply.c | 20 ++++++++++++++++++++
> t/t4133-apply-filenames.sh | 31 +++++++++++++++++++++++++++++++
> 2 files changed, 51 insertions(+)
>
> diff --git a/apply.c b/apply.c
> index c5d4fe7887..0fa6a15a65 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,
Nit: for "--- /dev/null" this prints "names an old file dev/null", but
the users never write "dev/null", that's what's left after one leading
component is stripped. And "--- a/f" gives "names an old file f". The
only way to reach here is a "---" before "new file mode", so can't we
name that instead of printing the old name? Same for the deleted case
with "+++" before "deleted file mode".
Thanks,
Siddharth
> + 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 751a01f92f..ff48e68da0 100755
> --- a/t/t4133-apply-filenames.sh
> +++ b/t/t4133-apply-filenames.sh
> @@ -146,4 +146,35 @@ test_expect_success 'name the empty "diff --git" line in the error' '
> reject_empty_git_header later_line.diff 3
> '
>
> +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
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [Outreachy PATCH v2 2/2] apply: reject new and deleted files that have both names
2026-11-15 12:47 ` Siddharth Asthana
@ 2026-11-16 18:06 ` Tanga Joris
0 siblings, 0 replies; 22+ messages in thread
From: Tanga Joris @ 2026-11-16 18:06 UTC (permalink / raw)
To: Siddharth Asthana, git
Cc: phillip.wood, gitster, jackrawlings2011, christian.couder,
kaartic.sivaraam, usmanakinyemi202
Hi Siddharth,
On Sun, 15 Nov 2026 at 15:47, Siddharth Asthana
<siddharthasthana31@gmail.com> wrote:
> Nit: for "--- /dev/null" this prints "names an old file dev/null", but
> the users never write "dev/null", that's what's left after one leading
> component is stripped. And "--- a/f" gives "names an old file f". The
> only way to reach here is a "---" before "new file mode", so can't we
> name that instead of printing the old name? Same for the deleted case
> with "+++" before "deleted file mode".
Alright, in v3 the error will name the "---" line before "new file
mode", and the "+++" line before "deleted file mode".
Thanks,
Tanga
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [Outreachy PATCH v2 1/2] apply: reject a patch after an empty "diff --git" line
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:39 ` 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
2 siblings, 1 reply; 22+ messages in thread
From: Siddharth Asthana @ 2026-11-15 12:39 UTC (permalink / raw)
To: Tanga Joris, git
Cc: phillip.wood, gitster, jackrawlings2011, christian.couder,
kaartic.sivaraam, usmanakinyemi202
On 14/11/26 06:50, Tanga Joris wrote:
> 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"
> line, parse_git_diff_header() stops right after the "diff --git"
> line. find_header() then treats it as an empty git header and keeps
> looking, as it has done since 206de27efa (git-apply: ignore empty git
> headers, 2005-06-12). It finds the "--- /dev/null" and "+++ b/<path>"
> lines and reads the rest as a traditional patch.
>
> But parse_git_diff_header() has already set old_name and new_name
> from the "diff --git" line. These names stay in the patch.
> parse_traditional_patch() sets is_new for "--- /dev/null", but it
> does not clear old_name. So "git apply --check" dies on the assertion
> that 5c47f4c6e7 (builtin-apply: accept patch to an empty file,
> 2008-05-17) added to check_preimage():
>
> check_preimage: Assertion `patch->is_new <= 0' failed.
>
> The stale new_name causes the same crash when a deletion with such a
> header is applied with "git apply -R". When such a patch modifies a
> 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.
>
> Reported-by: Gingrspacecadet <jackrawlings2011@gmail.com>
> Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
> Signed-off-by: Tanga Joris <tangajoris@gmail.com>
> ---
> Changes since v1:
> - 1/2 no longer reads the patch that follows an empty "diff --git"
> line. It reports the empty header as an error instead, as Phillip
> and Junio suggested:
>
> error: corrupt patch: empty "diff --git" header at <file>:<line>
>
> - A blank line after "diff --git" in a patch that modifies a file is
> now rejected too, as Phillip suggested. v1 and master apply it
> without its "old mode" and "new mode" lines.
> - One case that works on master is now rejected: a "diff --git" line
> followed by other text and then a non-git patch.
> - Dropped the test for an empty "diff --git" section followed by
> another section. That case behaves as on master, but the test fails
> under SANITIZE=leak, because of a leak in git_header_name() that is
> already on master. Also dropped the subdirectory test, which only
> made sense while the patch was applied.
The leak is actually in parse_git_diff_header(), we overwrite
patch->def_name without freeing the old value, so it leaks when the
function runs again for the next "diff --git". A free() before the
assignment fixes it, and both callers zero the patch first so it's safe.
Can't we do that in a prep patch and keep the test?
Nit: the notes under "---" describe both patches, so a cover letter with
"--cover-letter --range-diff=<v2>" would be nicer for v3.
Thanks,
Siddharth
> - 2/2: the code is unchanged. Its commit message now says why patches
> made by Git are not affected.
>
> apply.c | 13 +++++-
> t/t4133-apply-filenames.sh | 86 ++++++++++++++++++++++++++++++++++++++
> 2 files changed, 98 insertions(+), 1 deletion(-)
>
> diff --git a/apply.c b/apply.c
> index f00b7ba4d3..c5d4fe7887 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -1604,6 +1604,7 @@ 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;
> @@ -1644,6 +1645,7 @@ static int find_header(struct apply_state *state,
> * or mode change, so we handle that specially
> */
> if (!memcmp("diff --git ", line, 11)) {
> + int linenr = state->linenr;
> int git_hdr_len = parse_git_diff_header(&state->root,
> state->patch_input_file,
> &state->linenr,
> @@ -1651,8 +1653,10 @@ static int find_header(struct apply_state *state,
> size, patch);
> if (git_hdr_len < 0)
> return -128;
> - if (git_hdr_len <= len)
> + if (git_hdr_len <= len) {
> + empty_git_hdr_linenr = linenr;
> continue;
> + }
> *hdrsize = git_hdr_len;
> return offset;
> }
> @@ -1670,6 +1674,13 @@ 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;
> diff --git a/t/t4133-apply-filenames.sh b/t/t4133-apply-filenames.sh
> index 3cab1038cf..751a01f92f 100755
> --- a/t/t4133-apply-filenames.sh
> +++ b/t/t4133-apply-filenames.sh
> @@ -60,4 +60,90 @@ test_expect_success 'apply diff with old filename missing from headers' '
> test_grep "lacks filename information" err
> '
>
> +reject_empty_git_header () {
> + file=$1 &&
> + line=$2 &&
> + shift 2 &&
> + test_must_fail git apply --check "$@" "$file" 2>err &&
> + test_grep "corrupt patch: empty \"diff --git\" header at $file:$line" err
> +}
> +
> +test_expect_success 'reject a new file with a blank line after "diff --git"' '
> + cat >blank_line.diff <<-\EOF &&
> + diff --git a/f b/f
> +
> + new file mode 100644
> + index 0000000..d00491f
> + --- /dev/null
> + +++ b/f
> + @@ -0,0 +1 @@
> + +1
> + EOF
> + reject_empty_git_header blank_line.diff 1 &&
> + test_must_fail git apply blank_line.diff &&
> + test_path_is_missing f
> +'
> +
> +test_expect_success 'reject an indented "new file mode" line' '
> + cat >indented_mode.diff <<-\EOF &&
> + diff --git a/f b/f
> + new file mode 100644
> + index 0000000..d00491f
> + --- /dev/null
> + +++ b/f
> + @@ -0,0 +1 @@
> + +1
> + EOF
> + reject_empty_git_header indented_mode.diff 1
> +'
> +
> +test_expect_success 'reject a deletion with a blank line after "diff --git"' '
> + cat >blank_line_delete.diff <<-\EOF &&
> + diff --git a/f b/f
> +
> + deleted file mode 100644
> + index d00491f..0000000
> + --- a/f
> + +++ /dev/null
> + @@ -1 +0,0 @@
> + -1
> + EOF
> + reject_empty_git_header blank_line_delete.diff 1 &&
> + reject_empty_git_header blank_line_delete.diff 1 -R
> +'
> +
> +test_expect_success 'reject a modification with a blank line after "diff --git"' '
> + test_when_finished "rm -f g" &&
> + echo 1 >g &&
> + cat >blank_line_modify.diff <<-\EOF &&
> + diff --git a/g b/g
> +
> + old mode 100644
> + new mode 100755
> + index d00491f..0cfbf08
> + --- a/g
> + +++ b/g
> + @@ -1 +1 @@
> + -1
> + +2
> + EOF
> + reject_empty_git_header blank_line_modify.diff 1 &&
> + echo 1 >expect &&
> + test_cmp expect g
> +'
> +
> +test_expect_success 'name the empty "diff --git" line in the error' '
> + cat >later_line.diff <<-\EOF &&
> + some text before the patch
> +
> + diff --git a/f b/f
> + some text after it
> + --- /dev/null
> + +++ b/f
> + @@ -0,0 +1 @@
> + +1
> + EOF
> + reject_empty_git_header later_line.diff 3
> +'
> +
> test_done
>
> base-commit: a018953688f1b10bddf91bff8747068f5f4746a4
^ permalink raw reply [flat|nested] 22+ messages in thread* Re: [Outreachy PATCH v2 1/2] apply: reject a patch after an empty "diff --git" line
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
0 siblings, 0 replies; 22+ messages in thread
From: Tanga Joris @ 2026-11-16 17:54 UTC (permalink / raw)
To: Siddharth Asthana, git
Cc: phillip.wood, gitster, jackrawlings2011, christian.couder,
kaartic.sivaraam, usmanakinyemi202
Hi Siddharth,
Thank you for reviewing this.
On Sun, 15 Nov 2026 at 15:39, Siddharth Asthana
<siddharthasthana31@gmail.com> wrote:
> The leak is actually in parse_git_diff_header(), we overwrite
> patch->def_name without freeing the old value, so it leaks when the
> function runs again for the next "diff --git". A free() before the
> assignment fixes it, and both callers zero the patch first so it's safe.
> Can't we do that in a prep patch and keep the test?
Yes, I tried it. With the free(), the test passes under SANITIZE=leak,
so I'll add it as a preparatory patch in v3 and keep the test.
> Nit: the notes under "---" describe both patches, so a cover letter with
> "--cover-letter --range-diff=<v2>" would be nicer for v3.
Alright, I'll send v3 with a cover letter and a range-diff.
Thanks,
Tanga
^ permalink raw reply [flat|nested] 22+ messages in thread
* [Outreachy PATCH v3 0/3] apply: reject corrupt "diff --git" headers
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:39 ` [Outreachy PATCH v2 1/2] apply: reject a patch after an empty "diff --git" line Siddharth Asthana
@ 2026-11-17 0:43 ` 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
` (2 more replies)
2 siblings, 3 replies; 22+ messages in thread
From: Tanga Joris @ 2026-11-17 0:43 UTC (permalink / raw)
To: git
Cc: siddharthasthana31, phillip.wood, gitster, jackrawlings2011,
christian.couder, kaartic.sivaraam, usmanakinyemi202, Tanga Joris
"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
^ permalink raw reply [flat|nested] 22+ messages in thread* [Outreachy PATCH v3 1/3] apply: free the old def_name in parse_git_diff_header()
2026-11-17 0:43 ` [Outreachy PATCH v3 0/3] apply: reject corrupt "diff --git" headers Tanga Joris
@ 2026-11-17 0:43 ` 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
2 siblings, 0 replies; 22+ messages in thread
From: Tanga Joris @ 2026-11-17 0:43 UTC (permalink / raw)
To: git
Cc: siddharthasthana31, phillip.wood, gitster, jackrawlings2011,
christian.couder, kaartic.sivaraam, usmanakinyemi202, Tanga Joris
parse_git_diff_header() sets patch->def_name from the "diff --git"
line, without freeing the name it already holds. find_header() calls
it again for each "diff --git" line it finds, with the same patch. So
when an empty "diff --git" line is followed by another "diff --git"
line, the first name leaks.
Free the old name before setting the new one. Both callers start with
a zeroed patch, so def_name is NULL the first time.
Add a test that such a patch still applies. Without this change, the
test fails under SANITIZE=leak.
Suggested-by: Siddharth Asthana <siddharthasthana31@gmail.com>
Signed-off-by: Tanga Joris <tangajoris@gmail.com>
---
apply.c | 1 +
t/t4133-apply-filenames.sh | 18 ++++++++++++++++++
2 files changed, 19 insertions(+)
diff --git a/apply.c b/apply.c
index f00b7ba4d3..0847835357 100644
--- a/apply.c
+++ b/apply.c
@@ -1373,6 +1373,7 @@ int parse_git_diff_header(struct strbuf *root,
* or removing or adding empty files), so we get
* the default name from the header.
*/
+ free(patch->def_name);
patch->def_name = git_header_name(p_value, line, len);
if (patch->def_name && root->len) {
char *s = xstrfmt("%s%s", root->buf, patch->def_name);
diff --git a/t/t4133-apply-filenames.sh b/t/t4133-apply-filenames.sh
index 3cab1038cf..f8158faec0 100755
--- a/t/t4133-apply-filenames.sh
+++ b/t/t4133-apply-filenames.sh
@@ -60,4 +60,22 @@ test_expect_success 'apply diff with old filename missing from headers' '
test_grep "lacks filename information" err
'
+test_expect_success 'ignore an empty "diff --git" line before a git patch' '
+ 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
+ git apply empty_section.diff &&
+ echo 2 >expect &&
+ test_cmp expect g
+'
+
test_done
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [Outreachy PATCH v3 2/3] apply: reject a patch after an empty "diff --git" line
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-17 0:43 ` Tanga Joris
2026-11-17 0:43 ` [Outreachy PATCH v3 3/3] apply: reject new and deleted files that have both names Tanga Joris
2 siblings, 0 replies; 22+ messages in thread
From: Tanga Joris @ 2026-11-17 0:43 UTC (permalink / raw)
To: git
Cc: siddharthasthana31, phillip.wood, gitster, jackrawlings2011,
christian.couder, kaartic.sivaraam, usmanakinyemi202, Tanga Joris
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"
line, parse_git_diff_header() stops right after the "diff --git"
line. find_header() then treats it as an empty git header and keeps
looking, as it has done since 206de27efa (git-apply: ignore empty git
headers, 2005-06-12). It finds the "--- /dev/null" and "+++ b/<path>"
lines and reads the rest as a traditional patch.
But parse_git_diff_header() has already set old_name and new_name
from the "diff --git" line. These names stay in the patch.
parse_traditional_patch() sets is_new for "--- /dev/null", but it
does not clear old_name. So "git apply --check" dies on the assertion
that 5c47f4c6e7 (builtin-apply: accept patch to an empty file,
2008-05-17) added to check_preimage():
check_preimage: Assertion `patch->is_new <= 0' failed.
The stale new_name causes the same crash when a deletion with such a
header is applied with "git apply -R". When such a patch modifies a
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.
Reported-by: Gingrspacecadet <jackrawlings2011@gmail.com>
Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Tanga Joris <tangajoris@gmail.com>
---
apply.c | 13 +++++-
t/t4133-apply-filenames.sh | 86 ++++++++++++++++++++++++++++++++++++++
2 files changed, 98 insertions(+), 1 deletion(-)
diff --git a/apply.c b/apply.c
index 0847835357..77a665a71a 100644
--- a/apply.c
+++ b/apply.c
@@ -1605,6 +1605,7 @@ 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;
@@ -1645,6 +1646,7 @@ static int find_header(struct apply_state *state,
* or mode change, so we handle that specially
*/
if (!memcmp("diff --git ", line, 11)) {
+ int linenr = state->linenr;
int git_hdr_len = parse_git_diff_header(&state->root,
state->patch_input_file,
&state->linenr,
@@ -1652,8 +1654,10 @@ static int find_header(struct apply_state *state,
size, patch);
if (git_hdr_len < 0)
return -128;
- if (git_hdr_len <= len)
+ if (git_hdr_len <= len) {
+ empty_git_hdr_linenr = linenr;
continue;
+ }
*hdrsize = git_hdr_len;
return offset;
}
@@ -1671,6 +1675,13 @@ 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;
diff --git a/t/t4133-apply-filenames.sh b/t/t4133-apply-filenames.sh
index f8158faec0..b6b9195924 100755
--- a/t/t4133-apply-filenames.sh
+++ b/t/t4133-apply-filenames.sh
@@ -60,6 +60,92 @@ test_expect_success 'apply diff with old filename missing from headers' '
test_grep "lacks filename information" err
'
+reject_empty_git_header () {
+ file=$1 &&
+ line=$2 &&
+ shift 2 &&
+ test_must_fail git apply --check "$@" "$file" 2>err &&
+ test_grep "corrupt patch: empty \"diff --git\" header at $file:$line" err
+}
+
+test_expect_success 'reject a new file with a blank line after "diff --git"' '
+ cat >blank_line.diff <<-\EOF &&
+ diff --git a/f b/f
+
+ new file mode 100644
+ index 0000000..d00491f
+ --- /dev/null
+ +++ b/f
+ @@ -0,0 +1 @@
+ +1
+ EOF
+ reject_empty_git_header blank_line.diff 1 &&
+ test_must_fail git apply blank_line.diff &&
+ test_path_is_missing f
+'
+
+test_expect_success 'reject an indented "new file mode" line' '
+ cat >indented_mode.diff <<-\EOF &&
+ diff --git a/f b/f
+ new file mode 100644
+ index 0000000..d00491f
+ --- /dev/null
+ +++ b/f
+ @@ -0,0 +1 @@
+ +1
+ EOF
+ reject_empty_git_header indented_mode.diff 1
+'
+
+test_expect_success 'reject a deletion with a blank line after "diff --git"' '
+ cat >blank_line_delete.diff <<-\EOF &&
+ diff --git a/f b/f
+
+ deleted file mode 100644
+ index d00491f..0000000
+ --- a/f
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -1
+ EOF
+ reject_empty_git_header blank_line_delete.diff 1 &&
+ reject_empty_git_header blank_line_delete.diff 1 -R
+'
+
+test_expect_success 'reject a modification with a blank line after "diff --git"' '
+ test_when_finished "rm -f g" &&
+ echo 1 >g &&
+ cat >blank_line_modify.diff <<-\EOF &&
+ diff --git a/g b/g
+
+ old mode 100644
+ new mode 100755
+ index d00491f..0cfbf08
+ --- a/g
+ +++ b/g
+ @@ -1 +1 @@
+ -1
+ +2
+ EOF
+ reject_empty_git_header blank_line_modify.diff 1 &&
+ echo 1 >expect &&
+ test_cmp expect g
+'
+
+test_expect_success 'name the empty "diff --git" line in the error' '
+ cat >later_line.diff <<-\EOF &&
+ some text before the patch
+
+ diff --git a/f b/f
+ some text after it
+ --- /dev/null
+ +++ b/f
+ @@ -0,0 +1 @@
+ +1
+ EOF
+ 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 &&
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread* [Outreachy PATCH v3 3/3] apply: reject new and deleted files that have both names
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-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 ` Tanga Joris
2 siblings, 0 replies; 22+ messages in thread
From: Tanga Joris @ 2026-11-17 0:43 UTC (permalink / raw)
To: git
Cc: siddharthasthana31, phillip.wood, gitster, jackrawlings2011,
christian.couder, kaartic.sivaraam, usmanakinyemi202, Tanga Joris
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. 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.
Helped-by: Siddharth Asthana <siddharthasthana31@gmail.com>
Signed-off-by: Tanga Joris <tangajoris@gmail.com>
---
apply.c | 18 ++++++++++++++++++
t/t4133-apply-filenames.sh | 31 +++++++++++++++++++++++++++++++
2 files changed, 49 insertions(+)
diff --git a/apply.c b/apply.c
index 77a665a71a..b920dbbbc7 100644
--- a/apply.c
+++ b/apply.c
@@ -1467,6 +1467,24 @@ 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 has \"---\" before \"new file mode\" at %s:%d"),
+ patch_input_file, patch->extension_linenr);
+ else
+ 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 has \"+++\" before \"deleted file mode\" at %s:%d"),
+ patch_input_file, patch->extension_linenr);
+ else
+ error(_("git diff header has \"+++\" before \"deleted file mode\" (line %d)"),
+ patch->extension_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 b6b9195924..b85dcb57bd 100755
--- a/t/t4133-apply-filenames.sh
+++ b/t/t4133-apply-filenames.sh
@@ -164,4 +164,35 @@ test_expect_success 'ignore an empty "diff --git" line before a git patch' '
test_cmp expect g
'
+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 "has \"---\" before \"new file mode\" at old_before_new.diff:4" 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 "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 "has \"+++\" before \"deleted file mode\" at new_before_delete.diff:4" err
+'
+
test_done
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread