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

Re: [PATCH] evaluate the second argument of ALLOC_GROW only once

From
Johannes Sixt <j6t@kdbg.org>
Date
May 16, 2026, 06:55 UTC
Message-ID
<9ce768d4-0cbf-4494-a1d3-55fd3b05b61e@kdbg.org>
In-Reply-To
<20260515195049.GA149960@coredump.intra.peff.net>
Am 15.05.26 um 21:50 schrieb Jeff King:
Show 13 quoted lines
> On Fri, May 15, 2026 at 03:08:18PM -0400, Jeff King wrote:
> 
>> On Fri, May 15, 2026 at 08:16:50PM +0200, René Scharfe wrote:
>>
>>> +		size_t alloc_grow_new_alloc_; \
>>> +		if (st_alloc_nr((nr), (alloc), &alloc_grow_new_alloc_)) { \
>>> +			alloc = alloc_grow_new_alloc_; \
>>> +			REALLOC_ARRAY(x, alloc_grow_new_alloc_); \
>>>  		} \
>>
>> What happens if a caller passes in an argument that isn't a size_t?
>> We'll check for overflow in the size_t space, and then truncate it when
>> we assign to alloc, I think.
> 
> Hmm, playing with it and looking a little closer, I think we don't end
> up overflowing the buffer because you use the size_t for
> REALLOC_ARRAY(). So the result is big, but then "alloc" is truncated.
Protect against double-evaluation of "alloc", too, using
	size_t *palloc = &(alloc);

and use *palloc in the two places, then all callers are forced to work with a size_t as third argument. Don't know what the damage would be, though.

-- Hannes
Previous: René ScharfeNext: Jeff King
Message 7 of 8 in “evaluate the second argument of ALLOC_GROW only once”
  1. evaluate the second argument of ALLOC_GROW only onceRené Scharfe, May 15, 2026
  2. Jeff KingMay 15, 2026
  3. Jeff KingMay 15, 2026
  4. René ScharfeMay 15, 2026
  5. Jeff KingMay 16, 2026
  6. René ScharfeMay 16, 2026
  7. Johannes SixtMay 16, 2026
  8. Jeff KingMay 19, 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.