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

Re: To "const char *" and cast on free(), or "char *" and no cast...

From
EWEric Wong <e@80x24.org>
Date
Oct 14, 2021, 23:36 UTC
Message-ID
<20211014233616.GA3348@dcvr>
In-Reply-To
<87mtnbfk0g.fsf@evledraar.gmail.com>
Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:
> On Thu, Oct 14 2021, Phillip Wood wrote:
> 
> [Changed $subject]
Thanks, I might not've noticed this if you hadn't.
Show 17 quoted lines
> > On 14/10/2021 01:10, Ævar Arnfjörð Bjarmason wrote:
> >> The "checkout" command is one of the main sources of leaks in the test
> >> suite, let's fix the common ones by not leaking from the "struct
> >> branch_info".
> >> Doing this is rather straightforward, albeit verbose, we need to
> >> xstrdup() constant strings going into the struct, and free() the ones
> >> we clobber as we go along.
> >
> > It's great to see these leaks being fixed. I wonder though if it would
> > be better to change the structure definition so that 'name' and 'path' 
> > are no longer 'const'. That would be a better reflection of the new
> > regime.[...]
> 
> I think this is the right thing to do, but I'm not quite sure. There was
> a thread at it here:
> 
>     https://lore.kernel.org/git/YUZG0D5ayEWd7MLP@carlos-mbp.lan/

I'd much prefer we keep const-ness for safety and documentation purposes.

Show 6 quoted lines
> Where I chimed in and suggested exactly what you're saying here, but the
> consensus seemed to go the other way, and if you grep:
> 
>     git grep -F 'free((char *)'
> 
> You can see that we use this pattern pretty widely.

I've been using unions to workaround APIs like free(3) for many years:

static inline void deconst_free(const void *ptr)
{
	/* this initializer is a C99-ism */
	union { const void *in; void *out; } deconst = { .in = ptr };
	free(deconst.out);
}
Previous: Junio C HamanoNext: Phillip Wood
Message 7 of 12 in “checkout: fix "branch info" memory leaks”
  1. checkout: fix "branch info" memory leaksÆvar Arnfjörð Bjarmason, Oct 14, 2021
  2. Phillip WoodOct 14, 2021
  3. To "const char *" and cast on free(), or "char *" and no cast...Ævar Arnfjörð Bjarmason, Oct 14, 2021
  4. Junio C HamanoOct 14, 2021
  5. Phillip WoodOct 15, 2021
  6. Junio C HamanoOct 15, 2021
  7. Eric WongOct 14, 2021
  8. Phillip WoodOct 15, 2021
  9. checkout: fix "branch info" memory leaksÆvar Arnfjörð Bjarmason, Oct 21, 2021
  10. Phillip WoodOct 24, 2021
  11. checkout: fix "branch info" memory leaksÆvar Arnfjörð Bjarmason, Nov 3, 2021
  12. checkout: fix "branch info" memory leaksÆvar Arnfjörð Bjarmason, Nov 16, 2021

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.