All of lore.kernel.org
 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 v4 1/2] apply: reject an empty "diff --git" header
Date: Wed, 25 Nov 2026 09:27:38 -0800	[thread overview]
Message-ID: <xmqqo6bc7red.fsf@gitster.g> (raw)
In-Reply-To: <20261125011002.589285-2-tangajoris@gmail.com> (Tanga Joris's message of "Wed, 25 Nov 2026 04:10:01 +0300")

Tanga Joris <tangajoris@gmail.com> writes:

> When 206de27efa was written, Git printed the "diff --git" line before
> running diff, so a path with nothing really changed could end up
> with just that line. ec1fcc16af (Show original and resulting blob
> object info in diff output, 2005-10-07) added the "index" line, and a
> pure rename or mode change has its own header lines. So today's Git
> does not write an empty "diff --git" header, and one in a patch means
> the patch is corrupt. Report it as an error, instead of skipping it.

This explains nicely why we no longer need to ignore empty 'diff --git'
headers specifically and rather we should actively diagnose them as
errors.

> diff --git a/apply.c b/apply.c
> index 677c01dc02..b28c1401f4 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -1645,6 +1645,7 @@ static int find_header(struct apply_state *state,
>  		 * or mode change, so we handle that specially
>  		 */
>  		if (!memcmp("diff --git ", line, 11)) {
> +			int linenr = state->linenr;
>  			int git_hdr_len = parse_git_diff_header(&state->root,
>  								state->patch_input_file,
>  								&state->linenr,
> @@ -1652,8 +1653,11 @@ static int find_header(struct apply_state *state,
>  								size, patch);
>  			if (git_hdr_len < 0)
>  				return -128;
> -			if (git_hdr_len <= len)
> -				continue;
> +			if (git_hdr_len <= len) {
> +				error(_("corrupt patch: empty \"diff --git\" header at %s:%d"),
> +				      state->patch_input_file, linenr);
> +				return -128;
> +			}
>  			*hdrsize = git_hdr_len;
>  			return offset;
>  		}

Saving linenr before calling parse_git_diff_header() makes sure the
diagnostic points at the "diff --git" line itself rather than the
unrecognized line that terminated header parsing.  In apply_patch(),
'.patch_input_file' is always initialized (to the filename or "<stdin>")
before parse_chunk() calls find_header(), and returning -128 from here
causes apply_patch() to clean up via free_patch() without leaking
'.def_name', '.old_name', or '.new_name'.

One small pre-existing edge case worth noting is that the earlier "if
(size < len + 6) break;" check in find_header() still runs before the
"diff --git " check, so a trailing bare "diff --git" line at EOF with
fewer than six bytes after it will still hit EOF without this error.

Not something this patch necessarily needs to fix #leftoverbits.

> diff --git a/t/t4133-apply-filenames.sh b/t/t4133-apply-filenames.sh
> index 3cab1038cf..73d3bc4e08 100755
> --- a/t/t4133-apply-filenames.sh
> +++ b/t/t4133-apply-filenames.sh
> @@ -60,4 +60,108 @@ test_expect_success 'apply diff with old filename missing from headers' '
>  	test_grep "lacks filename information" err
>  '
>  
> +reject_empty_git_header () {
> +	file=$1 &&
> +	line=$2 &&
> +	shift 2 &&
> +	test_must_fail git apply --check "$@" "$file" 2>err &&
> +	test_grep "corrupt patch: empty \"diff --git\" header at $file:$line" err
> +}
> +
> +test_expect_success 'reject a new file with a blank line after "diff --git"' '
> ...

The new helper and tests in t/t4133-apply-filenames.sh cover creation,
deletion (both forward and with -R), modification with mode lines, non-1
line numbers, and consecutive "diff --git" lines cleanly.

Looking good.

  reply	other threads:[~2026-11-25 17:27 UTC|newest]

Thread overview: 32+ 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
2026-11-18 23:32           ` Tanga Joris
2026-11-19 20:28             ` Junio C Hamano
2026-11-20  0:17               ` Tanga Joris
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
2026-11-25  1:10       ` [Outreachy PATCH v4 0/2] apply: reject corrupt "diff --git" headers Tanga Joris
2026-11-25  1:10         ` [Outreachy PATCH v4 1/2] apply: reject an empty "diff --git" header Tanga Joris
2026-11-25 17:27           ` Junio C Hamano [this message]
2026-11-25 23:38             ` Tanga Joris
2026-12-02  2:18               ` [PATCH] apply: reject a "diff --git" line at the end of the input Tanga Joris
2026-11-25  1:10         ` [Outreachy PATCH v4 2/2] 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=xmqqo6bc7red.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.