From: Eric Wong <e@80x24.org>
To: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Cc: phillip.wood@dunelm.org.uk, git@vger.kernel.org,
"Junio C Hamano" <gitster@pobox.com>, "Jeff King" <peff@peff.net>,
"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>,
"Elijah Newren" <newren@gmail.com>
Subject: Re: To "const char *" and cast on free(), or "char *" and no cast...
Date: Thu, 14 Oct 2021 23:36:16 +0000 [thread overview]
Message-ID: <20211014233616.GA3348@dcvr> (raw)
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.
> > 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.
> 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);
}
next prev parent reply other threads:[~2021-10-14 23:36 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-14 0:10 [PATCH] checkout: fix "branch info" memory leaks Ævar Arnfjörð Bjarmason
2021-10-14 9:36 ` Phillip Wood
2021-10-14 19:54 ` To "const char *" and cast on free(), or "char *" and no cast Ævar Arnfjörð Bjarmason
2021-10-14 20:22 ` Junio C Hamano
2021-10-15 10:03 ` Phillip Wood
2021-10-15 16:00 ` Junio C Hamano
2021-10-14 23:36 ` Eric Wong [this message]
2021-10-15 9:50 ` Phillip Wood
2021-10-21 20:16 ` [PATCH v2] checkout: fix "branch info" memory leaks Ævar Arnfjörð Bjarmason
2021-10-24 18:30 ` Phillip Wood
2021-11-03 11:36 ` [PATCH v3] " Ævar Arnfjörð Bjarmason
2021-11-16 18:27 ` [PATCH v4] " Ævar Arnfjörð Bjarmason
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211014233616.GA3348@dcvr \
--to=e@80x24.org \
--cc=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=newren@gmail.com \
--cc=pclouds@gmail.com \
--cc=peff@peff.net \
--cc=phillip.wood@dunelm.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).