{"thread":{"id":"66739","subject":"[PATCH] apply: reject binary hunks whose size does not fit in an int","startedAt":"2026-11-01T17:24:56Z","lastAt":"2026-11-01T17:24:56Z","messageCount":1,"participants":["Elia Pinto"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"557084","messageId":"20261101172457.399774-1-gitter.spiros@gmail.com","threadId":"66739","inReplyTo":null,"subject":"[PATCH] apply: reject binary hunks whose size does not fit in an int","fromName":"Elia Pinto","fromEmail":"gitter.spiros@gmail.com","sentAt":"2026-11-01T17:24:56Z","receivedAt":"2026-11-01T17:24:56Z","isPatch":true,"sender":{"key":"gitter.spiros@gmail.com","avatar":"https://avatars.githubusercontent.com/u/158490?v=4"},"body":"In parse_binary_hunk(), the declared size of a binary hunk (\"literal N\"\nor \"delta N\") is read into an \"unsigned long\" and then stored in\nstruct fragment.size, which is an \"int\".\n\nReject binary hunks whose declared size does not fit in an \"int\",\ntreating them as corrupt patches before allocation and inflation, just\nas done for other malformed hunks.\n\nAdd a test with a hunk that declares a size of 3000000000. It runs\n\"git apply\" with GIT_ALLOC_LIMIT set, so that without this fix the\noversized allocation dies instead of the patch being reported as\ncorrupt, and the test fails.\n\nSigned-off-by: Elia Pinto <gitter.spiros@gmail.com>\n---\n apply.c                 |  4 ++++\n t/t4103-apply-binary.sh | 15 +++++++++++++++\n 2 files changed, 19 insertions(+)\n\ndiff --git a/apply.c b/apply.c\nindex a0b7119c63..731955ad09 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -2115,6 +2115,10 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,\n \t\tsize -= llen;\n \t}\n \n+\t/* frag->size is an int */\n+\tif (origlen > INT_MAX)\n+\t\tgoto corrupt;\n+\n \tCALLOC_ARRAY(frag, 1);\n \tfrag->patch = inflate_it(data, hunk_size, origlen);\n \tfrag->free_patch = 1;\ndiff --git a/t/t4103-apply-binary.sh b/t/t4103-apply-binary.sh\nindex bdd4ccf9e3..70d01aa1ab 100755\n--- a/t/t4103-apply-binary.sh\n+++ b/t/t4103-apply-binary.sh\n@@ -199,4 +199,19 @@ test_expect_success 'reject unrecognized binary diff' '\n \tEOF\n \ttest_cmp expect err\n '\n+\n+test_expect_success 'reject binary diff with size exceeding INT_MAX' '\n+\tcat >patch.big <<-\\EOF &&\n+\tdiff --git a/f b/f\n+\tnew file mode 100644\n+\tindex 0000000..7898192\n+\tGIT binary patch\n+\tliteral 3000000000\n+\tHcmV<n00001\n+\n+\tEOF\n+\ttest_must_fail env GIT_ALLOC_LIMIT=1m git apply patch.big 2>err &&\n+\ttest_grep \"error: corrupt binary patch\" err\n+'\n+\n test_done\n\nbase-commit: 50fe5cc63f9cd6e89b87a2c9ccf96a444f4cfdb7\nprerequisite-patch-id: 7adb31b4c9809dff36602bd05e0798d21a72e629\nprerequisite-patch-id: 3f37dc40500ff000618807bfa583bd31990716c2\nprerequisite-patch-id: 0b6bd0f4812d2f3dc805049c69a4ac25b708cc5c\n-- \n2.53.0\n\n\n"}]}