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

Re: [PATCH v2 1/1] zlib.c: use size_t for size

From
Ramsay Jones <ramsay@ramsayjones.plus.com>
Date
Oct 14, 2018, 15:03 UTC
Message-ID
<1e3233f4-ea40-6fff-0951-6b4885f88445@ramsayjones.plus.com>
In-Reply-To
<20181014025207.GA1527@sigill.intra.peff.net>
On 14/10/18 03:52, Jeff King wrote:
Show 38 quoted lines
> On Sun, Oct 14, 2018 at 03:16:36AM +0100, Ramsay Jones wrote:
> 
>> diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
>> index b059b86aee..3b5f2c38b3 100644
>> --- a/builtin/pack-objects.c
>> +++ b/builtin/pack-objects.c
>> @@ -269,12 +269,12 @@ static void copy_pack_data(struct hashfile *f,
>>  		off_t len)
>>  {
>>  	unsigned char *in;
>> -	unsigned long avail;
>> +	size_t avail;
>>  
>>  	while (len) {
>>  		in = use_pack(p, w_curs, offset, &avail);
>>  		if (avail > len)
>> -			avail = (unsigned long)len;
>> +			avail = xsize_t(len);
> 
> We don't actually care about truncation here. The idea is that we take a
> bite-sized chunk via use_pack, and loop as necessary. So mod-2^32
> truncation via a cast would be bad (we might not make forward progress),
> but truncating to SIZE_MAX would be fine.
> 
> That said, we know that would not truncate here, because we must
> strictly be shrinking "avail", which was already a size_t (unless "len"
> is negative, but then we are really screwed ;) ).
> 
> So I kind of wonder if a comment would be better than xsize_t here.
> Something like:
> 
>   if (avail > len) {
> 	/*
> 	 * This can never truncate because we know that len is smaller
> 	 * than avail, which is already a size_t.
> 	 */
> 	avail = (size_t)len;
>   }
Heh, you are, of course, correct! (that will learn me[1]). :-D

Hmm, let's see if I can muster the enthusiasm to do all that testing again!

ATB, Ramsay Jones

[1] Since I started with my patch, when I had finished 'paring it back', the result didn't have this xsize_t() call. In order to make the result 'v2 + SZEDER's patch' (which I thought was quite neat) I added this call right at the end. :-P

Previous: Jeff KingNext: Jeff King
Message 12 of 19 in “zlib.c: use size_t for size”
  1. zlib.c: use size_t for sizeJunio C Hamano, Oct 12, 2018
  2. Johannes SchindelinOct 12, 2018
  3. Junio C HamanoOct 12, 2018
  4. Johannes SchindelinOct 12, 2018
  5. Ramsay JonesOct 12, 2018
  6. 1/1 zlib.c: use size_t for sizetboegi@web.de, Oct 12, 2018
  7. SZEDER GáborOct 12, 2018
  8. Torsten BögershausenOct 13, 2018
  9. Ramsay JonesOct 14, 2018
  10. Ramsay JonesOct 14, 2018
  11. Jeff KingOct 14, 2018
  12. Ramsay JonesOct 14, 2018
  13. Jeff KingOct 15, 2018
  14. Ramsay JonesOct 15, 2018
  15. Junio C HamanoOct 15, 2018
  16. Torsten BögershausenOct 15, 2018
  17. Jeff KingOct 13, 2018
  18. Jeff KingOct 13, 2018
  19. Johannes SixtOct 13, 2018

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.