All of lore.kernel.org
 help / color / mirror / Atom feed
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 0/3] apply: reject corrupt "diff --git" headers
Date: Tue, 17 Nov 2026 03:43:45 +0300	[thread overview]
Message-ID: <20261117004348.806694-1-tangajoris@gmail.com> (raw)
In-Reply-To: <20261114012055.127905-1-tangajoris@gmail.com>

"git apply --check" dies on an assertion in check_preimage() for some
patches with a broken "diff --git" header. This series reports these
patches as errors instead.

1/3 fixes a leak in parse_git_diff_header().
2/3 rejects a patch that is read under an empty "diff --git" line,
like a "diff --git" line with a blank line after it.
3/3 rejects a new file with a "---" line before "new file mode", and
a deleted file with a "+++" line before "deleted file mode".

Changes since v2:
 - Added 1/3, which frees patch->def_name before setting it, as
   Siddharth suggested. It brings back the test for an empty
   "diff --git" line followed by another one, which v2 dropped.
 - 2/3 is unchanged, apart from where its tests sit in the file.
 - 3/3 names the "---" or "+++" line in the error, instead of the
   file name, which was "dev/null" for "/dev/null", as Siddharth
   suggested.
 - Fixed a broken line in the 3/3 commit message.

Tanga Joris (3):
  apply: free the old def_name in parse_git_diff_header()
  apply: reject a patch after an empty "diff --git" line
  apply: reject new and deleted files that have both names

 apply.c                    |  32 ++++++++-
 t/t4133-apply-filenames.sh | 135 +++++++++++++++++++++++++++++++++++++
 2 files changed, 166 insertions(+), 1 deletion(-)

Range-diff against v2:
-:  ---------- > 1:  8136f837a9 apply: free the old def_name in parse_git_diff_header()
1:  3b802e3053 ! 2:  048574a48f apply: reject a patch after an empty "diff --git" line
    @@ t/t4133-apply-filenames.sh: test_expect_success 'apply diff with old filename mi
     +	reject_empty_git_header later_line.diff 3
     +'
     +
    - test_done
    + test_expect_success 'ignore an empty "diff --git" line before a git patch' '
    + 	test_when_finished "rm -f g" &&
    + 	echo 1 >g &&
2:  f2d40ed931 ! 3:  b3da07db98 apply: reject new and deleted files that have both names
    @@ Commit message
         lines, 2017-06-27) made apply report other conflicting header lines,
         instead of leaving them to an assertion. Do the same here. After the
         header is read, reject a new file that has an old name. Reject a
    -    deleted file that has a new name too.
    +    deleted file that has a new name too. The error names the "---" or
    +    "+++" line that came before the mode line. It does not print the
    +    name, because for "/dev/null" that would be "dev/null".
     
         Git always writes the mode line first, so patches made by Git are not
    -    affected. A patch with
    -    "+++ /dev/null" before "deleted file mode" used to apply without -R.
    -    It is now rejected too.
    +    affected. A patch with "+++ /dev/null" before "deleted file mode" used
    +    to apply without -R. It is now rejected too.
     
    +    Helped-by: Siddharth Asthana <siddharthasthana31@gmail.com>
         Signed-off-by: Tanga Joris <tangajoris@gmail.com>
     
      ## apply.c ##
    @@ apply.c: int parse_git_diff_header(struct strbuf *root,
      	}
     +	if (patch->is_new > 0 && patch->old_name) {
     +		if (patch_input_file)
    -+			error(_("git diff header for new file %s names an old file %s at %s:%d"),
    -+			      patch->new_name, patch->old_name,
    -+			      patch_input_file, *linenr);
    ++			error(_("git diff header has \"---\" before \"new file mode\" at %s:%d"),
    ++			      patch_input_file, patch->extension_linenr);
     +		else
    -+			error(_("git diff header for new file %s names an old file %s (line %d)"),
    -+			      patch->new_name, patch->old_name, *linenr);
    ++			error(_("git diff header has \"---\" before \"new file mode\" (line %d)"),
    ++			      patch->extension_linenr);
     +		return -128;
     +	}
     +	if (patch->is_delete > 0 && patch->new_name) {
     +		if (patch_input_file)
    -+			error(_("git diff header for deleted file %s names a new file %s at %s:%d"),
    -+			      patch->old_name, patch->new_name,
    -+			      patch_input_file, *linenr);
    ++			error(_("git diff header has \"+++\" before \"deleted file mode\" at %s:%d"),
    ++			      patch_input_file, patch->extension_linenr);
     +		else
    -+			error(_("git diff header for deleted file %s names a new file %s (line %d)"),
    -+			      patch->old_name, patch->new_name, *linenr);
    ++			error(_("git diff header has \"+++\" before \"deleted file mode\" (line %d)"),
    ++			      patch->extension_linenr);
     +		return -128;
     +	}
      	patch->is_toplevel_relative = 1;
    @@ apply.c: int parse_git_diff_header(struct strbuf *root,
      }
     
      ## t/t4133-apply-filenames.sh ##
    -@@ t/t4133-apply-filenames.sh: test_expect_success 'name the empty "diff --git" line in the error' '
    - 	reject_empty_git_header later_line.diff 3
    +@@ t/t4133-apply-filenames.sh: test_expect_success 'ignore an empty "diff --git" line before a git patch' '
    + 	test_cmp expect g
      '
      
     +for old in /dev/null a/f
    @@ t/t4133-apply-filenames.sh: test_expect_success 'name the empty "diff --git" lin
     +		+1
     +		EOF
     +		test_must_fail git apply --check old_before_new.diff 2>err &&
    -+		test_grep "header for new file f names an old file" err
    ++		test_grep "has \"---\" before \"new file mode\" at old_before_new.diff:4" err
     +	'
     +done
     +
    @@ t/t4133-apply-filenames.sh: test_expect_success 'name the empty "diff --git" lin
     +	-1
     +	EOF
     +	test_must_fail git apply --check new_before_delete.diff 2>err &&
    -+	test_grep "header for deleted file f names a new file" err &&
    ++	test_grep "has \"+++\" before \"deleted file mode\" at new_before_delete.diff:4" err &&
     +	test_must_fail git apply --check -R new_before_delete.diff 2>err &&
    -+	test_grep "header for deleted file f names a new file" err
    ++	test_grep "has \"+++\" before \"deleted file mode\" at new_before_delete.diff:4" err
     +'
     +
      test_done

base-commit: a018953688f1b10bddf91bff8747068f5f4746a4
-- 
2.43.0


  parent reply	other threads:[~2026-11-17  0:44 UTC|newest]

Thread overview: 26+ 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     ` Tanga Joris [this message]
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

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-1-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 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.