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

Re: [PATCH] revision: hang on to "freed" argv elements

From
Patrick Steinhardt <ps@pks.im>
Date
Sep 1, 2026, 11:08 UTC
Message-ID
<apayIuf9kXQcQPvS@pks.im>
In-Reply-To
<20260901092120.GA2979683@coredump.intra.peff.net>
On Tue, Sep 01, 2026 at 05:21:20AM -0400, Jeff King wrote:
> On Tue, Sep 01, 2026 at 10:51:26AM +0200, Patrick Steinhardt wrote:
[snip]
Show 16 quoted lines
> > > +static void mark_argv_for_free(struct rev_info *revs, const char *str)
> > > +{
> > > +	if (!str)
> > > +		return;
> > > +	strvec_push_nodup(&revs->argv_to_free, (char *)str);
> > > +}
> > 
> > Hm. Doesn't this mean that we take ownership of the string and then
> > eventually try to release it when releasing the vector? I wonder whether
> > this could introduce subtle lifetime issues where the caller passes a
> > non-heap-allocated string.
> 
> Yes, that's exactly the point. We are replacing a call to free() with
> one that passes ownership to a strvec which later frees it. If somebody
> is passing a non-heap string along with free_removed_argv_elements, then
> everything was already broken.
Fair.
Show 11 quoted lines
> > I don't think it's that bad when seeing where we use these. But I feel
> > like hiding this fact by marking the parameter as `const` is a bit of a
> > weird design choice. I'd much rather prefer we force this onto the
> > callers so that they are aware of this, but I haven't seen the end
> > result of that. So maybe it's just too ugly.
> 
> You can see the effect already in the diff. In the preimage all of the
> callers had to cast away const-ness in order to pass the string to
> free(). We could keep doing that here, but since this function has
> exactly one purpose (to free the string we pass it) it seems like a nice
> syntactic convenience to push the cast in here.
Okay, fair enough.
> Though you may want to look at the "2/1" I sent, which pushes the check
> for free_removed_argv_elements into this function. And then the cast and
> that check are side-by-side.
Makes sense, thanks!
Patrick
Previous: Jeff King
Message 7 of 7 in “[BUG] git stash show --src-prefix prints freed memory since 2.52.0”
  1. Nicolas Le CamAug 30, 2026
  2. revision: hang on to "freed" argv elementsJeff King, Sep 1, 2026
  3. 2/1 revision: simplify mark_argv_for_free() callersJeff King, Sep 1, 2026
  4. Junio C HamanoSep 1, 2026
  5. Patrick SteinhardtSep 1, 2026
  6. Jeff KingSep 1, 2026
  7. Patrick SteinhardtSep 1, 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.