Re: [PATCH 2/5] http: use `size_t` for byte position in fwrite_sha1_file
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Oct 17, 2026, 04:11 UTC
- Message-ID
- <xmqqv771aruf.fsf@gitster.g>
- In-Reply-To
- <395b47c0-6dc7-4a1b-b547-e5e68e7356f5@hogyros.de>
Simon Richter <Simon.Richter@hogyros.de> writes:
Show 5 quoted lines
>> I agree that changing the type of posn from int to size_t is the >> right move. > > It is a position in a file that is being read as binary data[1], so > off_t would be the best choice I think.
Interesting.
The function writes out eltsize * nmemb bytes (both of type size_t), which means the product could theoretically exceed the range of size_t. However, posn is used in xwrite() to offset into a contiguous in-core buffer starting at ptr:
ssize_t retval = xwrite(freq->localfile,
(char *) ptr + posn,
size - posn);Because we are indexing into an allocated in-core region, any valid offset must fit within size_t. If posn were to exceed the bounds of size_t, the pointer arithmetic ptr + posn would already be invalid. As posn only runs up to the allocation size of the region, it indeed should be measured in size_t, no?