From c62ab511febb1ce4ffcdb28732072460a1425a8f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Mon, 15 Jun 2026 09:07:06 +0000 Subject: [PATCH] zlib: properly clamp to uLong On platforms where `unsigned long` and `size_t` differ in bit size, we want to clamp the buffers we pass to zlib to the former's size, as per d05d666977 (git-zlib: handle data streams larger than 4GB, 2026-05-08). The logic introduced in that commit performs a clamping to the bits, though, which fails to do what is needed here: If too many bytes are available in the buffers, we need to clamp to the maximum value of an `unsigned long`. Otherwise, we ask zlib to use too small buffers, in the worst case using 0 as the size (think: a value whose 32 lowest bits are all zero). Signed-off-by: Johannes Schindelin --- git-zlib.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/git-zlib.c b/git-zlib.c index b91cb323aee916..1cd69c17c3cbfa 100644 --- a/git-zlib.c +++ b/git-zlib.c @@ -42,8 +42,10 @@ static void zlib_pre_call(git_zstream *s) { s->z.next_in = s->next_in; s->z.next_out = s->next_out; - s->z.total_in = (uLong)(s->total_in & ULONG_MAX_VALUE); - s->z.total_out = (uLong)(s->total_out & ULONG_MAX_VALUE); + s->z.total_in = (uLong)(s->total_in < ULONG_MAX_VALUE ? + s->total_in : ULONG_MAX_VALUE); + s->z.total_out = (uLong)(s->total_out < ULONG_MAX_VALUE ? + s->total_out : ULONG_MAX_VALUE); s->z.avail_in = zlib_buf_cap(s->avail_in); s->z.avail_out = zlib_buf_cap(s->avail_out); }