Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Tanga Joris <tangajoris@gmail.com>
Cc: git@vger.kernel.org,  siddharthasthana31@gmail.com,
	phillip.wood@dunelm.org.uk,  jackrawlings2011@gmail.com,
	christian.couder@gmail.com,  kaartic.sivaraam@gmail.com,
	usmanakinyemi202@gmail.com
Subject: Re: [Outreachy PATCH v3 1/3] apply: free the old def_name in parse_git_diff_header()
Date: Wed, 18 Nov 2026 12:51:17 -0800	[thread overview]
Message-ID: <xmqqh5hd50e2.fsf@gitster.g> (raw)
In-Reply-To: <20261117004348.806694-2-tangajoris@gmail.com> (Tanga Joris's message of "Tue, 17 Nov 2026 03:43:46 +0300")

Tanga Joris <tangajoris@gmail.com> writes:

> 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);

This looks seriously inadequate, though.

After seeing "diff --git" line and extracting patch->def_name by
calling git_header_name(), suppose there is an unrecognised line
(e.g., "diff --git a/f b/f" followed by "diff --git a/g b/g").  We
set patch->old_name and patch->new_name both to copy of
patch->def_name (e.g., "f" taken from "diff --git a/f b/f") and
patch->is_toplevel_relative set to 1 before we return from
parse_git_diff_header() to find_header().  Which causes
find_header() to "continue" when git_hdr_len <= len, without
clearing patch->old_name and patch->new_name and others.

The test below only "passes" because the bogus "diff --git a/g b/g"
line names the same file "g" used for the next one, I suspect.  If
you replace 'g' with 'f' on the bogus "diff --git" line, you will
see an "inconsistent old filename" error, so this change is not
making us ignore the bogus "diff --git" line, either.  Not that I
think it is a good idea to ignore such a corrupt patch input.

> +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
> +'

I do not think we should ignore such a bogus "diff --git" line;
shouldn't we diagnose it as a corrupt patch input?  I do not think
we are there yet without making find_header() do more work when
punting, though.

Thanks.

  reply	other threads:[~2026-11-18 20:51 UTC|newest]

Thread overview: 23+ 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       ` [Outreachy PATCH v3 1/3] apply: free the old def_name in parse_git_diff_header() Tanga Joris
2026-11-18 20:51         ` Junio C Hamano [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=xmqqh5hd50e2.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=jackrawlings2011@gmail.com \
    --cc=kaartic.sivaraam@gmail.com \
    --cc=phillip.wood@dunelm.org.uk \
    --cc=siddharthasthana31@gmail.com \
    --cc=tangajoris@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