git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 12/12] git-zlib: widen `git_deflate_bound()` to `size_t`

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Aug 5, 2026, 13:58 UTC
Message-ID
<7fe7ca44-f3c4-7e99-b7fe-81e8467e294e@gmx.de>
In-Reply-To
<anMBBW_arzuri4Qo@pks.im>
Hi Patrick,
On Wed, 5 Aug 2026, Patrick Steinhardt wrote:
Show 11 quoted lines
> On Thu, Jul 09, 2026 at 04:49:39PM +0000, Johannes Schindelin via GitGitGadget wrote:
> > From: Johannes Schindelin <johannes.schindelin@gmx.de>
> > 
> > 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.
> 
> Nit, feel free to ignore: I feel like all of these patches could've been
> squashed into a single one, as they're trivial enough.
I like them trivial and small ;-)
Show 8 quoted lines
> > 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.
> 
> A link or something like that to the formula would've helped here, as
> I'm not familiar with this mechanism.
Right. I added two references to the commit message.
Show 28 quoted lines
> 
> > diff --git a/git-zlib.c b/git-zlib.c
> > index d21adb3bf5..ebbbcc6d1a 100644
> > --- a/git-zlib.c
> > +++ b/git-zlib.c
> > @@ -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
> 
> So is the idea here that we estimate the highest number of bytes that
> the deflated size could end up with?

Precisely. And the formula in zlib is a bit complex, it calculates a "fixedlen" and a "storelen" for two different ways to represent the worst case size. But for large values, only `storelen` matters, therefore we can get away with a much simpler logic here.

FWIW zlib v1.3.2 added `deflateBound_z()`, which accepts `size_t` (or more precisely: `z_size_t`). However, v1.3.2 is only 7 months old, so I'll be retired by the time Debian stable gets it :-P

Ciao, Johannes

> 
> Patrick
> 
Previous: Patrick SteinhardtNext: Johannes Schindelin via GitGitGadget
Message 21 of 55 in “Next size_t stop: pack-objects/delta”
  1. 00/12 Next size_t stop: pack-objects/deltaJohannes Schindelin via GitGitGadget, Jul 9, 2026
  2. 01/12 diff-delta: widen `struct delta_index`' size fields to `size_t`Johannes Schindelin via GitGitGadget, Jul 9, 2026
  3. Patrick SteinhardtAug 5, 2026
  4. Johannes SchindelinAug 5, 2026
  5. 02/12 delta: widen `create_delta_index()` parameter to `size_t`Johannes Schindelin via GitGitGadget, Jul 9, 2026
  6. 03/12 pack-objects: widen delta-cache accounting to `size_t`Johannes Schindelin via GitGitGadget, Jul 9, 2026
  7. Patrick SteinhardtAug 5, 2026
  8. Johannes SchindelinAug 5, 2026
  9. 04/12 pack-objects: widen `free_unpacked()` return to `size_t`Johannes Schindelin via GitGitGadget, Jul 9, 2026
  10. Patrick SteinhardtAug 5, 2026
  11. 05/12 pack-objects: widen `mem_usage` and `try_delta()`'s out-param to `size_t`Johannes Schindelin via GitGitGadget, Jul 9, 2026
  12. 06/12 delta: widen `create_delta()` and `diff_delta()` to `size_t`Johannes Schindelin via GitGitGadget, Jul 9, 2026
  13. Patrick SteinhardtAug 5, 2026
  14. 07/12 packfile, git-zlib: widen `use_pack()` and zstream avail fields to `size_t`Johannes Schindelin via GitGitGadget, Jul 9, 2026
  15. 08/12 archive-zip: widen `zlib_deflate_raw()`'s maxsize local to `size_t`Johannes Schindelin via GitGitGadget, Jul 9, 2026
  16. 09/12 diff: widen `deflate_it()`'s bound local from int to `size_t`Johannes Schindelin via GitGitGadget, Jul 9, 2026
  17. 10/12 http-push: widen `start_put()`'s size local from `ssize_t` to `size_t`Johannes Schindelin via GitGitGadget, Jul 9, 2026
  18. 11/12 t/helper/test-pack-deltas: widen `do_compress()`'s maxsize local to `size_t`Johannes Schindelin via GitGitGadget, Jul 9, 2026
  19. 12/12 git-zlib: widen `git_deflate_bound()` to `size_t`Johannes Schindelin via GitGitGadget, Jul 9, 2026
  20. Patrick SteinhardtAug 5, 2026
  21. Johannes SchindelinAug 5, 2026
  22. 00/12 Next size_t stop: pack-objects/deltaJohannes Schindelin via GitGitGadget, Aug 5, 2026
  23. 02/12 delta: widen `create_delta_index()` parameter to `size_t`Johannes Schindelin via GitGitGadget, Aug 5, 2026
  24. 01/12 diff-delta: widen `struct delta_index`' size fields to `size_t`Johannes Schindelin via GitGitGadget, Aug 5, 2026
  25. Patrick SteinhardtAug 6, 2026
  26. 03/12 pack-objects: widen delta-cache accounting to `size_t`Johannes Schindelin via GitGitGadget, Aug 5, 2026
  27. 05/12 pack-objects: widen `mem_usage` and `try_delta()`'s out-param to `size_t`Johannes Schindelin via GitGitGadget, Aug 5, 2026
  28. 04/12 pack-objects: widen `free_unpacked()` return to `size_t`Johannes Schindelin via GitGitGadget, Aug 5, 2026
  29. 06/12 delta: widen `create_delta()` and `diff_delta()` to `size_t`Johannes Schindelin via GitGitGadget, Aug 5, 2026
  30. 07/12 packfile, git-zlib: widen `use_pack()` and zstream avail fields to `size_t`Johannes Schindelin via GitGitGadget, Aug 5, 2026
  31. Junio C HamanoAug 7, 2026
  32. Junio C HamanoAug 7, 2026
  33. 09/12 diff: widen `deflate_it()`'s bound local from int to `size_t`Johannes Schindelin via GitGitGadget, Aug 5, 2026
  34. 08/12 archive-zip: widen `zlib_deflate_raw()`'s maxsize local to `size_t`Johannes Schindelin via GitGitGadget, Aug 5, 2026
  35. 11/12 t/helper/test-pack-deltas: widen `do_compress()`'s maxsize local to `size_t`Johannes Schindelin via GitGitGadget, Aug 5, 2026
  36. 10/12 http-push: widen `start_put()`'s size local from `ssize_t` to `size_t`Johannes Schindelin via GitGitGadget, Aug 5, 2026
  37. 12/12 git-zlib: widen `git_deflate_bound()` to `size_t`Johannes Schindelin via GitGitGadget, Aug 5, 2026
  38. Patrick SteinhardtAug 6, 2026
  39. Junio C HamanoAug 6, 2026
  40. Junio C HamanoAug 10, 2026
  41. 00/13 Next size_t stop: pack-objects/deltaJohannes Schindelin via GitGitGadget, Aug 13, 2026
  42. 01/13 diff-delta: widen `struct delta_index`' size fields to `size_t`Johannes Schindelin via GitGitGadget, Aug 13, 2026
  43. 02/13 delta: widen `create_delta_index()` parameter to `size_t`Johannes Schindelin via GitGitGadget, Aug 13, 2026
  44. 03/13 pack-objects: widen delta-cache accounting to `size_t`Johannes Schindelin via GitGitGadget, Aug 13, 2026
  45. 04/13 pack-objects: widen `free_unpacked()` return to `size_t`Johannes Schindelin via GitGitGadget, Aug 13, 2026
  46. 05/13 pack-objects: widen `mem_usage` and `try_delta()`'s out-param to `size_t`Johannes Schindelin via GitGitGadget, Aug 13, 2026
  47. 07/13 packfile, git-zlib: widen `use_pack()` and zstream avail fields to `size_t`Johannes Schindelin via GitGitGadget, Aug 13, 2026
  48. 06/13 delta: widen `create_delta()` and `diff_delta()` to `size_t`Johannes Schindelin via GitGitGadget, Aug 13, 2026
  49. 08/13 archive-zip: widen `zlib_deflate_raw()`'s maxsize local to `size_t`Johannes Schindelin via GitGitGadget, Aug 13, 2026
  50. 09/13 diff: widen `deflate_it()`'s bound local from int to `size_t`Johannes Schindelin via GitGitGadget, Aug 13, 2026
  51. 10/13 http-push: widen `start_put()`'s size local from `ssize_t` to `size_t`Johannes Schindelin via GitGitGadget, Aug 13, 2026
  52. 12/13 git-zlib: widen `git_deflate_bound()` to `size_t`Johannes Schindelin via GitGitGadget, Aug 13, 2026
  53. 13/13 packfile: widen `unpack_object_header_buffer()` to `size_t`Johannes Schindelin via GitGitGadget, Aug 13, 2026
  54. 11/13 t/helper/test-pack-deltas: widen `do_compress()`'s maxsize local to `size_t`Johannes Schindelin via GitGitGadget, Aug 13, 2026
  55. Junio C HamanoAug 13, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.