]> git.ipfire.org Git - thirdparty/git.git/commitdiff
git-zlib: widen `git_deflate_bound()` to `size_t`
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Thu, 9 Jul 2026 16:49:39 +0000 (16:49 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 9 Jul 2026 21:55:24 +0000 (14:55 -0700)
All four `unsigned long`/`int`/`ssize_t` receivers across archive-zip,
diff, http-push and t/helper/test-pack-deltas were widened to `size_t`
in the prior commits, and remote-curl and fast-import were already
there. With every caller prepared, both the parameter and the return
type can now move without introducing any silent narrowing.

For inputs above zlib's `uLong` range (i.e. >4 GiB on platforms where
`uLong` is 32-bit, notably 64-bit Windows), defer to zlib's stored-block
formula (the same fallback it would itself use for an unknown stream
state) plus the worst-case wrapper overhead. The existing path through
`deflateBound()` is unchanged for inputs that fit.

Assisted-by: Opus 4.7
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-zlib.c
git-zlib.h

index d21adb3bf5b15e85c3baaed25fdb204c869b9632..ebbbcc6d1a5077384ff2a07366c1f65fcf9325a3 100644 (file)
@@ -167,9 +167,21 @@ int git_inflate(git_zstream *strm, int flush)
        return status;
 }
 
-unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
+size_t git_deflate_bound(git_zstream *strm, size_t size)
 {
-       return deflateBound(&strm->z, size);
+#if SIZE_MAX > ULONG_MAX
+       if (size > maximum_unsigned_value_of_type(uLong))
+               /*
+                * deflateBound() takes uLong, which is 32-bit on
+                * Windows. For inputs above that range, return zlib's
+                * stored-block formula (the conservative path it would
+                * itself use for an unknown stream state) plus the
+                * worst-case wrapper overhead.
+                */
+               return size + (size >> 5) + (size >> 7) + (size >> 11)
+                       + 7 + 18;
+#endif
+       return deflateBound(&strm->z, (uLong)size);
 }
 
 void git_deflate_init(git_zstream *strm, int level)
index 0b24b15bd05f7f2adb0e5ab113bcf6b22e125316..9248d11ca9622cd8dc79b5e3c6ca608d827f50a2 100644 (file)
@@ -25,6 +25,6 @@ void git_deflate_end(git_zstream *);
 int git_deflate_abort(git_zstream *);
 int git_deflate_end_gently(git_zstream *);
 int git_deflate(git_zstream *, int flush);
-unsigned long git_deflate_bound(git_zstream *, unsigned long);
+size_t git_deflate_bound(git_zstream *, size_t);
 
 #endif /* GIT_ZLIB_H */