]> git.ipfire.org Git - thirdparty/git.git/commit
apply: avoid leaking abandoned git-header state
authorZephyr Yao <zot.zot.yao@gmail.com>
Thu, 2 Jul 2026 04:17:59 +0000 (00:17 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 2 Jul 2026 17:32:21 +0000 (10:32 -0700)
commitffe5c33a4423b78e893e340db7387aec8befada7
treec98c6fbb595598bc2e6e860409549ff96b7748a4
parent94f057755b7941b321fd11fec1b2e3ca5313a4e0
apply: avoid leaking abandoned git-header state

When find_header() sees a "diff --git" line, it calls
parse_git_diff_header() to parse the git-style extended header. That parser
updates the caller's struct patch as it goes, filling in the default name,
old/new names, and new/delete state.

But not every "diff --git" line found while scanning is ultimately accepted
as the patch header. If parse_git_diff_header() returns a length that covers
only the "diff --git" line, find_header() continues scanning for another
header. In that case the partially parsed git-header state must not interfere
with the later traditional "---" / "+++" header.

Leaving that state behind can combine incompatible metadata from the
abandoned git header and the later traditional header. For example, after:

diff --git a/foo b/foo

--- /dev/null
+++ b/foo
@@ -0,0 +1 @@
+x

the abandoned git header can leave an old name in the patch, while the
traditional header marks the patch as creating a new file. That impossible
state later trips the check_preimage() assertion that a creation patch should
not have a preimage.

Parse a candidate git header into a temporary patch and line number. Commit
that temporary state to the real patch only when the git header is actually
accepted; otherwise release it and keep scanning with the original patch
state unchanged.

Also reject an empty parsed default name from the "diff --git" line.
An empty patch->def_name is not a valid pathname, and should not be
used later as a fallback when old_name and new_name are missing.

Add regression tests for both the empty default-name case and the non-empty
abandoned-header case above.

Co-authored-by: Mahya SamDaliri <ms3539@njit.edu>
Signed-off-by: Mahya SamDaliri <ms3539@njit.edu>
Co-authored-by: Haotian Zhang <haotian.zhang@njit.edu>
Signed-off-by: Haotian Zhang <haotian.zhang@njit.edu>
Co-authored-by: Martin Kellogg <martin.kellogg@njit.edu>
Signed-off-by: Martin Kellogg <martin.kellogg@njit.edu>
Signed-off-by: Zephyr Yao <zhihao.yao@njit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply.c
t/t4100-apply-stat.sh