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
Jeff King <peff@peff.net>
Date
May 19, 2026, 00:41 UTC
Message-ID
<20260519004143.GA1612961@coredump.intra.peff.net>
In-Reply-To
<9ce768d4-0cbf-4494-a1d3-55fd3b05b61e@kdbg.org>
On Sat, May 16, 2026 at 08:55:54AM +0200, Johannes Sixt wrote:
Show 11 quoted lines
> > 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.

I think it would be nice if all ALLOC_GROW() callers used a size_t, and then we checked the size_t computation for overflow. But from a rough guess (taking your suggestion and trying to compile) we'd need to adjust ~200 callers.

And it's not just a straight conversion:
  1. Sometimes the ability to represent a negative value is important,
     and each site has to be audited. If we could agree on a "as big as
     size_t but signed" type, that might help.
  2. Changing the alloc variable type without the matching "nr" can
     actually make things worse. We tend to catch overflow-by-1 for
     signed types incidentally because it results in a stupidly large
     allocation request. But if made our allocations correct, then we
     might overflow on "nr" and start writing to some huge negative
     offset before the array.

So I think it would be a fair bit of work, though I would feel better about the resulting state.

-Peff
Previous: Johannes Sixt
Message 8 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.