{"thread":{"id":"65273","subject":"[GSoC PATCH] apply: report input location in binary and garbage patch errors","startedAt":"2026-03-17T00:44:54Z","lastAt":"2026-03-17T16:21:29Z","messageCount":4,"participants":["Jialong Wang","Karthik Nayak","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"539190","messageId":"20260317002235.6121-1-jerrywang183@yahoo.com","threadId":"65273","inReplyTo":"20260317002235.6121-1-jerrywang183.ref@yahoo.com","subject":"[GSoC PATCH] apply: report input location in binary and garbage patch errors","fromName":"Jialong Wang","fromEmail":"jerrywang183@yahoo.com","sentAt":"2026-03-17T00:22:35Z","receivedAt":"2026-03-17T00:44:54Z","isPatch":true,"sender":{"key":"jerrywang183@yahoo.com","avatar":null},"body":"Several binary parsing paths in apply.c still report only line\nnumbers. When more than one patch input is fed to a single\ninvocation, that does not tell the user which input the line belongs\nto.\n\nReport the patch input location for corrupt and unrecognized binary\npatches, as well as the \"patch with only garbage\" case, and update\nthe related tests.\n\nSigned-off-by: Jialong Wang <jerrywang183@yahoo.com>\n---\n apply.c                 | 10 ++++++----\n t/t4100-apply-stat.sh   | 12 ++++++++++++\n t/t4103-apply-binary.sh | 20 +++++++++++++++++++-\n 3 files changed, 37 insertions(+), 5 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex 700809f3e6..84b4a569c5 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -2110,8 +2110,8 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,\n  corrupt:\n \tfree(data);\n \t*status_p = -1;\n-\terror(_(\"corrupt binary patch at line %d: %.*s\"),\n-\t      state->linenr-1, llen-1, buffer);\n+\terror(_(\"corrupt binary patch at %s:%d: %.*s\"),\n+\t      state->patch_input_file, state->linenr-1, llen-1, buffer);\n \treturn NULL;\n }\n \n@@ -2147,7 +2147,8 @@ static int parse_binary(struct apply_state *state,\n \tforward = parse_binary_hunk(state, &buffer, &size, &status, &used);\n \tif (!forward && !status)\n \t\t/* there has to be one hunk (forward hunk) */\n-\t\treturn error(_(\"unrecognized binary patch at line %d\"), state->linenr-1);\n+\t\treturn error(_(\"unrecognized binary patch at %s:%d\"),\n+\t\t\t     state->patch_input_file, state->linenr-1);\n \tif (status)\n \t\t/* otherwise we already gave an error message */\n \t\treturn status;\n@@ -2309,7 +2310,8 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si\n \t\t */\n \t\tif ((state->apply || state->check) &&\n \t\t    (!patch->is_binary && !metadata_changes(patch))) {\n-\t\t\terror(_(\"patch with only garbage at line %d\"), state->linenr);\n+\t\t\terror(_(\"patch with only garbage at %s:%d\"),\n+\t\t\t      state->patch_input_file, state->linenr);\n \t\t\treturn -128;\n \t\t}\n \t}\ndiff --git a/t/t4100-apply-stat.sh b/t/t4100-apply-stat.sh\nindex b3d93d8ed6..8393076469 100755\n--- a/t/t4100-apply-stat.sh\n+++ b/t/t4100-apply-stat.sh\n@@ -125,4 +125,16 @@ test_expect_success 'applying a patch with an invalid mode reports the input' '\n \tEOF\n \ttest_cmp expect err\n '\n+\n+test_expect_success 'applying a patch with only garbage reports the input' '\n+\tcat >garbage.patch <<-\\EOF &&\n+\tdiff --git a/f b/f\n+\t--- a/f\n+\t+++ b/f\n+\tthis is garbage\n+\tEOF\n+\ttest_must_fail git apply garbage.patch 2>err &&\n+\techo \"error: patch with only garbage at garbage.patch:4\" >expect &&\n+\ttest_cmp expect err\n+'\n test_done\ndiff --git a/t/t4103-apply-binary.sh b/t/t4103-apply-binary.sh\nindex 8e302a5a57..f2d41e06bc 100755\n--- a/t/t4103-apply-binary.sh\n+++ b/t/t4103-apply-binary.sh\n@@ -179,6 +179,24 @@ test_expect_success PERL_TEST_HELPERS 'reject truncated binary diff' '\n \t\" <patch >patch.trunc &&\n \n \tdo_reset &&\n-\ttest_must_fail git apply patch.trunc\n+\ttest_must_fail git apply patch.trunc 2>err &&\n+\tline=$(awk \"END { print NR + 1 }\" patch.trunc) &&\n+\tgrep \"error: corrupt binary patch at patch.trunc:$line: \" err\n+'\n+\n+test_expect_success 'reject unrecognized binary diff' '\n+\tcat >patch.bad <<-\\EOF &&\n+\tdiff --git a/f b/f\n+\tnew file mode 100644\n+\tindex 0000000..7898192\n+\tGIT binary patch\n+\tbogus\n+\tEOF\n+\ttest_must_fail git apply patch.bad 2>err &&\n+\tcat >expect <<-\\EOF &&\n+\terror: unrecognized binary patch at patch.bad:4\n+\terror: No valid patches in input (allow with \"--allow-empty\")\n+\tEOF\n+\ttest_cmp expect err\n '\n test_done\n-- \n2.51.0\n\n"},{"id":"539208","messageId":"CAOLa=ZR8SZRN_xD29gshW3sujncuvhVSVh_9w=XpCHTcCf13Gg@mail.gmail.com","threadId":"65273","inReplyTo":"20260317002235.6121-1-jerrywang183@yahoo.com","subject":"Re: [GSoC PATCH] apply: report input location in binary and garbage patch errors","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-03-17T09:40:57Z","receivedAt":"2026-03-17T09:40:59Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Jialong Wang <jerrywang183@yahoo.com> writes:\n\n> Several binary parsing paths in apply.c still report only line\n> numbers. When more than one patch input is fed to a single\n> invocation, that does not tell the user which input the line belongs\n> to.\n>\n> Report the patch input location for corrupt and unrecognized binary\n> patches, as well as the \"patch with only garbage\" case, and update\n> the related tests.\n>\n> Signed-off-by: Jialong Wang <jerrywang183@yahoo.com>\n> ---\n>  apply.c                 | 10 ++++++----\n>  t/t4100-apply-stat.sh   | 12 ++++++++++++\n>  t/t4103-apply-binary.sh | 20 +++++++++++++++++++-\n>  3 files changed, 37 insertions(+), 5 deletions(-)\n>\n> diff --git a/apply.c b/apply.c\n> index 700809f3e6..84b4a569c5 100644\n> --- a/apply.c\n> +++ b/apply.c\n> @@ -2110,8 +2110,8 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,\n>   corrupt:\n>  \tfree(data);\n>  \t*status_p = -1;\n> -\terror(_(\"corrupt binary patch at line %d: %.*s\"),\n> -\t      state->linenr-1, llen-1, buffer);\n> +\terror(_(\"corrupt binary patch at %s:%d: %.*s\"),\n> +\t      state->patch_input_file, state->linenr-1, llen-1, buffer);\n>  \treturn NULL;\n>  }\n>\n> @@ -2147,7 +2147,8 @@ static int parse_binary(struct apply_state *state,\n>  \tforward = parse_binary_hunk(state, &buffer, &size, &status, &used);\n>  \tif (!forward && !status)\n>  \t\t/* there has to be one hunk (forward hunk) */\n> -\t\treturn error(_(\"unrecognized binary patch at line %d\"), state->linenr-1);\n> +\t\treturn error(_(\"unrecognized binary patch at %s:%d\"),\n> +\t\t\t     state->patch_input_file, state->linenr-1);\n>  \tif (status)\n>  \t\t/* otherwise we already gave an error message */\n>  \t\treturn status;\n> @@ -2309,7 +2310,8 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si\n>  \t\t */\n>  \t\tif ((state->apply || state->check) &&\n>  \t\t    (!patch->is_binary && !metadata_changes(patch))) {\n> -\t\t\terror(_(\"patch with only garbage at line %d\"), state->linenr);\n> +\t\t\terror(_(\"patch with only garbage at %s:%d\"),\n> +\t\t\t      state->patch_input_file, state->linenr);\n>  \t\t\treturn -128;\n>  \t\t}\n>  \t}\n> diff --git a/t/t4100-apply-stat.sh b/t/t4100-apply-stat.sh\n> index b3d93d8ed6..8393076469 100755\n> --- a/t/t4100-apply-stat.sh\n> +++ b/t/t4100-apply-stat.sh\n> @@ -125,4 +125,16 @@ test_expect_success 'applying a patch with an invalid mode reports the input' '\n>  \tEOF\n>  \ttest_cmp expect err\n>  '\n> +\n> +test_expect_success 'applying a patch with only garbage reports the input' '\n> +\tcat >garbage.patch <<-\\EOF &&\n> +\tdiff --git a/f b/f\n> +\t--- a/f\n> +\t+++ b/f\n> +\tthis is garbage\n> +\tEOF\n> +\ttest_must_fail git apply garbage.patch 2>err &&\n> +\techo \"error: patch with only garbage at garbage.patch:4\" >expect &&\n> +\ttest_cmp expect err\n> +'\n>  test_done\n> diff --git a/t/t4103-apply-binary.sh b/t/t4103-apply-binary.sh\n> index 8e302a5a57..f2d41e06bc 100755\n> --- a/t/t4103-apply-binary.sh\n> +++ b/t/t4103-apply-binary.sh\n> @@ -179,6 +179,24 @@ test_expect_success PERL_TEST_HELPERS 'reject truncated binary diff' '\n>  \t\" <patch >patch.trunc &&\n>\n>  \tdo_reset &&\n> -\ttest_must_fail git apply patch.trunc\n> +\ttest_must_fail git apply patch.trunc 2>err &&\n> +\tline=$(awk \"END { print NR + 1 }\" patch.trunc) &&\n> +\tgrep \"error: corrupt binary patch at patch.trunc:$line: \" err\n> +'\n> +\n> +test_expect_success 'reject unrecognized binary diff' '\n> +\tcat >patch.bad <<-\\EOF &&\n> +\tdiff --git a/f b/f\n> +\tnew file mode 100644\n> +\tindex 0000000..7898192\n> +\tGIT binary patch\n> +\tbogus\n> +\tEOF\n> +\ttest_must_fail git apply patch.bad 2>err &&\n> +\tcat >expect <<-\\EOF &&\n> +\terror: unrecognized binary patch at patch.bad:4\n> +\terror: No valid patches in input (allow with \"--allow-empty\")\n> +\tEOF\n> +\ttest_cmp expect err\n>  '\n>  test_done\n> --\n> 2.51.0\n\nThis patch looks good to me!\n\nThanks for picking this up.\n\n- Karthik\n"},{"id":"539231","messageId":"xmqq5x6umobb.fsf@gitster.g","threadId":"65273","inReplyTo":"20260317002235.6121-1-jerrywang183@yahoo.com","subject":"Re: [GSoC PATCH] apply: report input location in binary and garbage patch errors","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-03-17T16:08:08Z","receivedAt":"2026-03-17T16:08:11Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jialong Wang <jerrywang183@yahoo.com> writes:\n\n> diff --git a/t/t4100-apply-stat.sh b/t/t4100-apply-stat.sh\n> index b3d93d8ed6..8393076469 100755\n> --- a/t/t4100-apply-stat.sh\n> +++ b/t/t4100-apply-stat.sh\n> @@ -125,4 +125,16 @@ test_expect_success 'applying a patch with an invalid mode reports the input' '\n>  \tEOF\n>  \ttest_cmp expect err\n>  '\n> +\n> +test_expect_success 'applying a patch with only garbage reports the input' '\n> +\tcat >garbage.patch <<-\\EOF &&\n> +\tdiff --git a/f b/f\n> +\t--- a/f\n> +\t+++ b/f\n> +\tthis is garbage\n> +\tEOF\n> +\ttest_must_fail git apply garbage.patch 2>err &&\n> +\techo \"error: patch with only garbage at garbage.patch:4\" >expect &&\n> +\ttest_cmp expect err\n> +'\n>  test_done\n\nWhat is this patch based on?  Can we have a consolidated either a\nsingle patch or a series of patches that form a single topic?\n\n> diff --git a/t/t4103-apply-binary.sh b/t/t4103-apply-binary.sh\n> index 8e302a5a57..f2d41e06bc 100755\n> --- a/t/t4103-apply-binary.sh\n> +++ b/t/t4103-apply-binary.sh\n> @@ -179,6 +179,24 @@ test_expect_success PERL_TEST_HELPERS 'reject truncated binary diff' '\n>  \t\" <patch >patch.trunc &&\n>  \n>  \tdo_reset &&\n> -\ttest_must_fail git apply patch.trunc\n> +\ttest_must_fail git apply patch.trunc 2>err &&\n> +\tline=$(awk \"END { print NR + 1 }\" patch.trunc) &&\n> +\tgrep \"error: corrupt binary patch at patch.trunc:$line: \" err\n> +'\n> +\n> +test_expect_success 'reject unrecognized binary diff' '\n> +\tcat >patch.bad <<-\\EOF &&\n> +\tdiff --git a/f b/f\n> +\tnew file mode 100644\n> +\tindex 0000000..7898192\n> +\tGIT binary patch\n> +\tbogus\n> +\tEOF\n> +\ttest_must_fail git apply patch.bad 2>err &&\n> +\tcat >expect <<-\\EOF &&\n> +\terror: unrecognized binary patch at patch.bad:4\n> +\terror: No valid patches in input (allow with \"--allow-empty\")\n> +\tEOF\n> +\ttest_cmp expect err\n>  '\n>  test_done\n"},{"id":"539233","messageId":"177376448032.71580.9145093498181847563@yahoo.com","threadId":"65273","inReplyTo":"xmqq5x6umobb.fsf@gitster.g","subject":"Re: [GSoC PATCH] apply: report input location in binary and garbage patch errors","fromName":"Jialong Wang","fromEmail":"jerrywang183@yahoo.com","sentAt":"2026-03-17T16:21:20Z","receivedAt":"2026-03-17T16:21:29Z","isPatch":true,"sender":{"key":"jerrywang183@yahoo.com","avatar":null},"body":"Hi Junio,\n\nYes, these follow-up patches are based on my earlier input-location\nreporting change in apply.c.\n\nI split the remaining error sites while working through them, but I\nagree they are better presented as a single topic. I'll reroll them as\na small series on top of the original patch.\n\nThanks,\nJialong\n"}]}