git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [GSoC PATCH] apply: report the location of corrupt patches

From
Karthik Nayak <karthik.188@gmail.com>
Date
Mar 16, 2026, 11:14 UTC
Message-ID
<CAOLa=ZTpfHUySnMgCFMnvo2JcRSv8zqFP-cLFSs+Ab5Cy2zsvg@mail.gmail.com>
In-Reply-To
<20260315231538.68586-1-jerrywang183@yahoo.com>
Jialong Wang <jerrywang183@yahoo.com> writes:
Show 6 quoted lines
> When parsing a corrupt patch, git apply reports only the line number.
> That does not tell the user which input the line number refers to.
>
> Include the patch input path in the error message so the reported
> location is easier to use.
>
Definitely a welcome change.
Show 19 quoted lines
> Add tests for both file input and standard input.
>
> Signed-off-by: Jialong Wang <jerrywang183@yahoo.com>
> ---
>  apply.c               |  3 ++-
>  t/t4100-apply-stat.sh | 16 +++++++++++++++-
>  2 files changed, 17 insertions(+), 2 deletions(-)
>
> diff --git a/apply.c b/apply.c
> index b6dd1066a0..904d1c3e55 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -1875,7 +1875,8 @@ static int parse_single_patch(struct apply_state *state,
>  		len = parse_fragment(state, line, size, patch, fragment);
>  		if (len <= 0) {
>  			free(fragment);
> -			return error(_("corrupt patch at line %d"), state->linenr);
> +			return error(_("corrupt patch at %s:%d"),
> +				     state->patch_input_file, state->linenr);

Okay so `prase_single_patch()` is called by `parse_chunk()` which is called by `apply_patch()`, which unconditionally sets `state->patch_input_file`. So this looks good.

Show 32 quoted lines
>  		}
>  		fragment->patch = line;
>  		fragment->size = len;
> diff --git a/t/t4100-apply-stat.sh b/t/t4100-apply-stat.sh
> index a5664f3eb3..f99e439688 100755
> --- a/t/t4100-apply-stat.sh
> +++ b/t/t4100-apply-stat.sh
> @@ -48,7 +48,21 @@ test_expect_success 'applying a hunk header which overflows fails' '
>  	+b
>  	EOF
>  	test_must_fail git apply patch 2>err &&
> -	echo "error: corrupt patch at line 4" >expect &&
> +	echo "error: corrupt patch at patch:4" >expect &&
> +	test_cmp expect err
> +'
> +
> +test_expect_success 'applying a hunk header which overflows from stdin fails' '
> +	cat >patch <<-\EOF &&
> +	diff -u a/file b/file
> +	--- a/file
> +	+++ b/file
> +	@@ -98765432109876543210 +98765432109876543210 @@
> +	-a
> +	+b
> +	EOF
> +	test_must_fail git apply <patch 2>err &&
> +	echo "error: corrupt patch at <stdin>:4" >expect &&
>  	test_cmp expect err
>  '
>  test_done
> --
> 2.51.0
Nit: It would also be nice to see that this does work with multiple
patch inputs, where one of them is corrupted.
Previous: Jialong WangNext: Jialong Wang
Message 2 of 14 in “apply: report the location of corrupt patches”
  1. apply: report the location of corrupt patchesJialong Wang, Mar 15, 2026
  2. Karthik NayakMar 16, 2026
  3. Jialong WangMar 16, 2026
  4. apply: report the location of corrupt patchesJialong Wang, Mar 16, 2026
  5. Junio C HamanoMar 16, 2026
  6. apply: report the location of corrupt patchesJialong Wang, Mar 16, 2026
  7. 0/3 apply: report input file for more parse errorsJialong Wang, Mar 17, 2026
  8. 2/3 apply: report input location in header parsing errorsJialong Wang, Mar 17, 2026
  9. 3/3 apply: report input location in binary and garbage patch errorsJialong Wang, Mar 17, 2026
  10. 1/3 apply: report the location of corrupt patchesJialong Wang, Mar 17, 2026
  11. Jialong WangMar 16, 2026
  12. Junio C HamanoMar 16, 2026
  13. apply: report input location in header parsing errorsJialong Wang, Mar 16, 2026
  14. apply: report input location in header parsing errorsJialong Wang, Mar 16, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.