Re: [PATCH 2/4] midx repack: avoid potential integer overflow on 64 bit systems
- From
Taylor Blau <me@ttaylorr.com>
- Date
- May 20, 2025, 17:59 UTC
- Message-ID
- <aCzDCEK7OhCYyAZa@nand.local>
- In-Reply-To
- <9f07da4fe71d21b14226d8f0132cd3c8600fba13.1747753388.git.phillip.wood@dunelm.org.uk>
On Tue, May 20, 2025 at 04:04:25PM +0100, Phillip Wood wrote:
Show 15 quoted lines
> From: Phillip Wood <phillip.wood@dunelm.org.uk> > > On a 64 bit system the calculation > > p->pack_size * pack_info[i].referenced_objects > > could overflow. If a pack file contains 2^28 objects with an average > compressed size of 1KB then the pack size will be 2^38B. If all of the > objects are referenced by the multi-pack index the sum above will > overflow. Avoid this by using shifted integer arithmetic and changing > the order of the calculation so that the pack size is divided by the > total number of objects in the pack before multiplying by the number of > objects referenced by the multi-pack index. Using a shift of 14 bits > should give reasonable accuracy while avoiding overflow for pack sizes > less that 1PB.
Ahhh, this renders some of comments on the previous patch moot. I think that this is a not-unreasonable concern to be addressing even on modern 64-bit systems, since I have definitely encountered packs that have on the order of ~2^28 objects in them.
I like this approach quite a bit, thanks!
Thanks, Taylor