From: Elia Pinto <gitter.spiros@gmail.com>
To: git@vger.kernel.org
Cc: Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Elia Pinto <gitter.spiros@gmail.com>,
Jialong Wang <jerrywang183@yahoo.com>,
Junio C Hamano <gitster@pobox.com>
Subject: [PATCH] apply: reject binary hunks whose size does not fit in an int
Date: Sun, 1 Nov 2026 18:24:56 +0100 [thread overview]
Message-ID: <20261101172457.399774-1-gitter.spiros@gmail.com> (raw)
In parse_binary_hunk(), the declared size of a binary hunk ("literal N"
or "delta N") is read into an "unsigned long" and then stored in
struct fragment.size, which is an "int".
Reject binary hunks whose declared size does not fit in an "int",
treating them as corrupt patches before allocation and inflation, just
as done for other malformed hunks.
Add a test with a hunk that declares a size of 3000000000. It runs
"git apply" with GIT_ALLOC_LIMIT set, so that without this fix the
oversized allocation dies instead of the patch being reported as
corrupt, and the test fails.
Signed-off-by: Elia Pinto <gitter.spiros@gmail.com>
---
apply.c | 4 ++++
t/t4103-apply-binary.sh | 15 +++++++++++++++
2 files changed, 19 insertions(+)
diff --git a/apply.c b/apply.c
index a0b7119c63..731955ad09 100644
--- a/apply.c
+++ b/apply.c
@@ -2115,6 +2115,10 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,
size -= llen;
}
+ /* frag->size is an int */
+ if (origlen > INT_MAX)
+ goto corrupt;
+
CALLOC_ARRAY(frag, 1);
frag->patch = inflate_it(data, hunk_size, origlen);
frag->free_patch = 1;
diff --git a/t/t4103-apply-binary.sh b/t/t4103-apply-binary.sh
index bdd4ccf9e3..70d01aa1ab 100755
--- a/t/t4103-apply-binary.sh
+++ b/t/t4103-apply-binary.sh
@@ -199,4 +199,19 @@ test_expect_success 'reject unrecognized binary diff' '
EOF
test_cmp expect err
'
+
+test_expect_success 'reject binary diff with size exceeding INT_MAX' '
+ cat >patch.big <<-\EOF &&
+ diff --git a/f b/f
+ new file mode 100644
+ index 0000000..7898192
+ GIT binary patch
+ literal 3000000000
+ HcmV<n00001
+
+ EOF
+ test_must_fail env GIT_ALLOC_LIMIT=1m git apply patch.big 2>err &&
+ test_grep "error: corrupt binary patch" err
+'
+
test_done
base-commit: 50fe5cc63f9cd6e89b87a2c9ccf96a444f4cfdb7
prerequisite-patch-id: 7adb31b4c9809dff36602bd05e0798d21a72e629
prerequisite-patch-id: 3f37dc40500ff000618807bfa583bd31990716c2
prerequisite-patch-id: 0b6bd0f4812d2f3dc805049c69a4ac25b708cc5c
--
2.53.0
reply other threads:[~2026-11-01 17:25 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261101172457.399774-1-gitter.spiros@gmail.com \
--to=gitter.spiros@gmail.com \
--cc=Johannes.Schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jerrywang183@yahoo.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox