# Fix for git apply --check crash grows into a leak fix in diff header parsing

_Review of the patch for a crash reported against git apply --check found a memory leak in parse_git_diff_header() and prompted requests to reword new error messages._

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

The thread began as a bug report from Gingrspacecadet, who said git apply --check crashed when checking a patch. The excerpt does not show the patch contents or the exact failure. Tanga Joris, Phillip Wood and Junio C Hamano also took part, and the thread has since moved to a proposed fix that has gone through at least two versions.

Siddharth Asthana reviewed the fix and made two points. First, he questioned a new message that says a patch "names an old file". For "--- /dev/null" it prints "dev/null", which is what remains after one leading path component is stripped and is not something users write. For "--- a/f" it prints "f". He noted the error can only be reached when a "---" line comes before "new file mode", and suggested the message name that condition instead of the old filename. The same applies to the deleted-file case, where "+++" comes before "deleted file mode".

Second, Asthana said the leak is actually in parse_git_diff_header(). It overwrites patch->def_name without freeing the old value, so it leaks when the function runs again for the next "diff --git" line. A free() before the assignment fixes it, and he said both callers zero the patch first, so this is safe. He asked whether that could go in a prep patch with the test kept.

He also asked that the next version use a cover letter, with --cover-letter and --range-diff against v2. The notes under the "---" line currently describe both patches. Whether the author adopts the split is not yet shown.
