From: Tanga Joris <tangajoris@gmail.com>
To: git@vger.kernel.org
Cc: siddharthasthana31@gmail.com, phillip.wood@dunelm.org.uk,
gitster@pobox.com, jackrawlings2011@gmail.com,
christian.couder@gmail.com, kaartic.sivaraam@gmail.com,
usmanakinyemi202@gmail.com, Tanga Joris <tangajoris@gmail.com>
Subject: [Outreachy PATCH v3 1/3] apply: free the old def_name in parse_git_diff_header()
Date: Tue, 17 Nov 2026 03:43:46 +0300 [thread overview]
Message-ID: <20261117004348.806694-2-tangajoris@gmail.com> (raw)
In-Reply-To: <20261117004348.806694-1-tangajoris@gmail.com>
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
next prev parent reply other threads:[~2026-11-17 0:44 UTC|newest]
Thread overview: 22+ 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 ` Tanga Joris [this message]
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
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=20261117004348.806694-2-tangajoris@gmail.com \
--to=tangajoris@gmail.com \
--cc=christian.couder@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jackrawlings2011@gmail.com \
--cc=kaartic.sivaraam@gmail.com \
--cc=phillip.wood@dunelm.org.uk \
--cc=siddharthasthana31@gmail.com \
--cc=usmanakinyemi202@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).