Skip to content

Commit ab911cf

Browse files
committed
git-zlib: widen git_deflate_bound() to size_t
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>
1 parent 617960d commit ab911cf

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

git-zlib.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,21 @@ int git_inflate(git_zstream *strm, int flush)
167167
return status;
168168
}
169169

170-
unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
170+
size_t git_deflate_bound(git_zstream *strm, size_t size)
171171
{
172-
return deflateBound(&strm->z, size);
172+
#if SIZE_MAX > ULONG_MAX
173+
if (size > maximum_unsigned_value_of_type(uLong))
174+
/*
175+
* deflateBound() takes uLong, which is 32-bit on
176+
* Windows. For inputs above that range, return zlib's
177+
* stored-block formula (the conservative path it would
178+
* itself use for an unknown stream state) plus the
179+
* worst-case wrapper overhead.
180+
*/
181+
return size + (size >> 5) + (size >> 7) + (size >> 11)
182+
+ 7 + 18;
183+
#endif
184+
return deflateBound(&strm->z, (uLong)size);
173185
}
174186

175187
void git_deflate_init(git_zstream *strm, int level)

git-zlib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ void git_deflate_end(git_zstream *);
2525
int git_deflate_abort(git_zstream *);
2626
int git_deflate_end_gently(git_zstream *);
2727
int git_deflate(git_zstream *, int flush);
28-
unsigned long git_deflate_bound(git_zstream *, unsigned long);
28+
size_t git_deflate_bound(git_zstream *, size_t);
2929

3030
#endif /* GIT_ZLIB_H */

0 commit comments

Comments
 (0)