From: Junio C Hamano <gitster@pobox.com>
To: Duy Nguyen <pclouds@gmail.com>
Cc: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
"Git Mailing List" <git@vger.kernel.org>,
"Michał Górny" <mgorny@gentoo.org>,
"Eric Sunshine" <sunshine@sunshineco.com>
Subject: Re: [PATCH] refactor various if (x) FREE_AND_NULL(x) to just FREE_AND_NULL(x)
Date: Fri, 17 Aug 2018 09:53:44 -0700 [thread overview]
Message-ID: <xmqqpnyhaq93.fsf@gitster-ct.c.googlers.com> (raw)
In-Reply-To: <20180817151012.GA20262@duynguyen.home> (Duy Nguyen's message of "Fri, 17 Aug 2018 17:10:12 +0200")
Duy Nguyen <pclouds@gmail.com> writes:
> Just fyi this seems to do the trick. Although I'm nowhere good at
> coccinelle to say if we should include this (or something like it)
>
> -- 8< --
> diff --git a/contrib/coccinelle/free.cocci b/contrib/coccinelle/free.cocci
> index 4490069df9..f8e018d104 100644
> --- a/contrib/coccinelle/free.cocci
> +++ b/contrib/coccinelle/free.cocci
> @@ -16,3 +16,9 @@ expression E;
> - free(E);
> + FREE_AND_NULL(E);
> - E = NULL;
> +
> +@@
> +expression E;
> +@@
> +- if (E) { FREE_AND_NULL(E); }
> ++ FREE_AND_NULL(E);
It is a bit sad that
- if (E)
FREE_AND_NULL(E);
is not sufficient to catch it. Shouldn't we be doing the same for
regular free(E) as well? IOW, like the attached patch.
contrib/coccinelle/free.cocci | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/contrib/coccinelle/free.cocci b/contrib/coccinelle/free.cocci
index 4490069df9..f748bcfe30 100644
--- a/contrib/coccinelle/free.cocci
+++ b/contrib/coccinelle/free.cocci
@@ -16,3 +16,27 @@ expression E;
- free(E);
+ FREE_AND_NULL(E);
- E = NULL;
+
+@@
+expression E;
+@@
+- if (E)
+ FREE_AND_NULL(E);
+
+@@
+expression E;
+@@
+- if (E) { free(E); }
++ free(E);
+
+@@
+expression E;
+@@
+- if (!E) { free(E); }
++ free(E);
+
+@@
+expression E;
+@@
+- if (E) { FREE_AND_NULL(E); }
++ FREE_AND_NULL(E);
next prev parent reply other threads:[~2018-08-17 16:53 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-17 9:17 [PATCH] gpg-interface.c: Fix potentially freeing NULL values Michał Górny
2018-08-17 9:28 ` Eric Sunshine
2018-08-17 9:40 ` Michał Górny
2018-08-17 13:02 ` [PATCH] refactor various if (x) FREE_AND_NULL(x) to just FREE_AND_NULL(x) Ævar Arnfjörð Bjarmason
2018-08-17 14:36 ` Duy Nguyen
2018-08-17 15:10 ` Duy Nguyen
2018-08-17 16:53 ` Junio C Hamano [this message]
2018-08-17 17:07 ` Junio C Hamano
2018-08-17 17:33 ` Jeff King
2018-08-17 17:39 ` Jeff King
2018-08-17 17:44 ` Jeff King
2018-08-17 18:29 ` Duy Nguyen
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=xmqqpnyhaq93.fsf@gitster-ct.c.googlers.com \
--to=gitster@pobox.com \
--cc=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=mgorny@gentoo.org \
--cc=pclouds@gmail.com \
--cc=sunshine@sunshineco.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.