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

Re: [PATCH] refactor various if (x) FREE_AND_NULL(x) to just FREE_AND_NULL(x)

From
Duy Nguyen <pclouds@gmail.com>
Date
Aug 17, 2018, 15:10 UTC
Message-ID
<20180817151012.GA20262@duynguyen.home>
In-Reply-To
<CACsJy8DH2tESV4xkCYutH=Ye37zGwifGdJhdnNOsRd+JusdOwg@mail.gmail.com>
On Fri, Aug 17, 2018 at 04:36:13PM +0200, Duy Nguyen wrote:
Show 11 quoted lines
> On Fri, Aug 17, 2018 at 3:05 PM Ævar Arnfjörð Bjarmason
> <avarab@gmail.com> wrote:
> >
> > Change the few conditional uses of FREE_AND_NULL(x) to be
> > unconditional. As noted in the standard[1] free(NULL) is perfectly
> > valid, so we might as well leave this check up to the C library.
> 
> I'm not trying to make you work more on this. But out of curiosity
> would coccinelle help catch this pattern? Szeder's recent work on
> running cocci automatically would help catch all future code like this
> if we could write an spatch.

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);
-- 8< --

--
Duy
Previous: Duy NguyenNext: Junio C Hamano
Message 6 of 12 in “gpg-interface.c: Fix potentially freeing NULL values”
  1. gpg-interface.c: Fix potentially freeing NULL valuesMichał Górny, Aug 17, 2018
  2. Eric SunshineAug 17, 2018
  3. Michał GórnyAug 17, 2018
  4. refactor various if (x) FREE_AND_NULL(x) to just FREE_AND_NULL(x)Ævar Arnfjörð Bjarmason, Aug 17, 2018
  5. Duy NguyenAug 17, 2018
  6. Duy NguyenAug 17, 2018
  7. Junio C HamanoAug 17, 2018
  8. Junio C HamanoAug 17, 2018
  9. Jeff KingAug 17, 2018
  10. Jeff KingAug 17, 2018
  11. Jeff KingAug 17, 2018
  12. Duy NguyenAug 17, 2018

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.