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

[PATCH] cocci: remove risky "if (!E) free(E)" conversion

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 11, 2026, 22:09 UTC
Message-ID
<xmqqld978mok.fsf@gitster.g>
The current cocci patches try to convert
	if (!E)
		free(E);
into an unconditional call to free(E), with the rationale
    cocci: detect useless free(3) calls
    Add a semantic patch for removing checks that cause free(3) to only be
    called with a NULL pointer, as that must be a programming mistake.

which came from ec6cd14c7a (cocci: detect useless free(3) calls, 2017-02-11).

Leaving _something_ in ALL.patch output to draw programmers' attention is a good thing, but this changes a piece of code that is originally a no-op to do something else, which may be even worse.

We could change it to
	if (!E)
		BUG("free(E) is certainly not what we meant to write");

to force programmers to think. But it probably is safer to just rewrite one form of no-op into a simpler form of no-op.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 tools/coccinelle/free.cocci | 10 ----------
 1 file changed, 10 deletions(-)
diff --git a/tools/coccinelle/free.cocci b/tools/coccinelle/free.cocci
index 03799e1908..3dfaae9dd8 100644
--- a/tools/coccinelle/free.cocci
+++ b/tools/coccinelle/free.cocci
@@ -8,16 +8,6 @@ expression E;
   commit_list_free(E);
 )
 
-@@
-expression E;
-@@
-- if (!E)
-(
-  free(E);
-|
-  commit_list_free(E);
-)
-
 @@
 expression E;
 @@
-- 
2.56.0-rc0-143-g1fea62d0ca
Next: Junio C Hamano
Message 1 of 5 in “cocci: remove risky "if (!E) free(E)" conversion”
  1. cocci: remove risky "if (!E) free(E)" conversionJunio C Hamano, Sep 11, 2026
  2. cocci: FREE_AND_NULL(E) is safe to call on NULLJunio C Hamano, Sep 11, 2026
  3. René ScharfeSep 12, 2026
  4. Junio C HamanoSep 12, 2026
  5. René ScharfeSep 13, 2026

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.