From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: phillip.wood@dunelm.org.uk
Cc: 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: To "const char *" and cast on free(), or "char *" and no cast...
Date: Thu, 14 Oct 2021 21:54:19 +0200 [thread overview]
Message-ID: <87mtnbfk0g.fsf@evledraar.gmail.com> (raw)
In-Reply-To: <8f87cdb9-b52b-8d1a-545d-ed3055c536c0@gmail.com>
On Thu, Oct 14 2021, Phillip Wood wrote:
[Changed $subject]
> 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/
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.
> It would also mean we could lose all the casts when freeing
> and there would be a compiler warning if a string literal is assigned
> to one of those fields.
What compiler/set of warnings gives you a warning when you do that? I
don't get warned on e.g.:
diff --git a/builtin/checkout.c b/builtin/checkout.c
index a32af16d5e4..d7053579bdf 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -94 +94 @@ struct branch_info {
- const char *name; /* The short name used */
+ char *name; /* The short name used */
@@ -110 +110 @@ static void branch_info_release(struct branch_info *info)
- free((char *)info->name);
+ free(info->name);
@@ -1107 +1107 @@ static int switch_branches(const struct checkout_opts *opts,
- new_branch_info->name = xstrdup("(empty)");
+ new_branch_info->name = "(empty)";
Now, what is really useful is making it a "char * const", especially
when hacking up these changes as you'll find all the assignments, but I
haven't found the general use in having that make it to a submitted
patch, since you need to assign somewhere, and those then need to be a
str[n]cpy() (except we banned.h it) or memcpy() with a cast...
next prev parent reply other threads:[~2021-10-14 20:07 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 ` Ævar Arnfjörð Bjarmason [this message]
2021-10-14 20:22 ` To "const char *" and cast on free(), or "char *" and no cast 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
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87mtnbfk0g.fsf@evledraar.gmail.com \
--to=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.
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 NNTP newsgroup(s).