# Two ways a malformed patch makes `git apply` die in check_preimage()

_Tanga Joris traced the reported `git apply --check` crash to header ordering and to empty git headers, and both cases hit the same assertion._

Section: Bugs. Edition of 2026-11-08. Written by an AI editor from the thread "Git crashed on `git apply --check`" (26 messages): https://gitlist.dev/t/66568

A user, Gingrspacecadet, reported that `git apply --check` crashed on a patch. Tanga Joris posted analyses of the cause.

In the first, the ordering of header lines matters. Git normally writes "new file mode" or "deleted file mode" before the "---" and "+++" lines. The "new file mode" line sets is_new, and gitdiff_oldname() then leaves old_name empty for "--- /dev/null". When the "---" line comes first, is_new is still 0, so gitdiff_verify_name() takes the name as old_name; for "--- /dev/null" that gives "dev/null", since one leading directory is stripped. The "new file mode" line then sets is_new, leaving a new file that also has an old name, and `git apply --check` dies on the assertion in check_preimage(). Likewise, "+++ /dev/null" before "deleted file mode" gives a deleted file with a new name, and `git apply -R` dies.

The second path involves an empty 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" line, parse_git_diff_header() stops right after it. find_header() treats this as an empty git header and keeps looking, as it has since 206de27efa (2005). It finds the "--- /dev/null" and "+++ b/<path>" lines and reads the rest as a traditional patch. But old_name and new_name were already set from the "diff --git" line, and parse_traditional_patch() does not clear old_name. The result is again a new file with an old name.

The excerpts show these as explanations of the crash, apparently accompanying patches, but do not show review or a merge decision.
