From d870565117a948be0b8a6ac48b7a862628ed18f1 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 11 Sep 2026 15:09:47 -0700 Subject: [PATCH 1/2] cocci: remove risky "if (!E) free(E)" conversion 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 --- 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; @@ From 32814b69d05714c42def93b44a6ff8f1a242e8fe Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 11 Sep 2026 15:21:35 -0700 Subject: [PATCH 2/2] cocci: FREE_AND_NULL(E) is safe to call on NULL Just like we allow calling free(E) without checking if E is not NULL, it is safe to call FREE_AND_NULL(E) unconditionally. Signed-off-by: Junio C Hamano --- tools/coccinelle/free.cocci | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/coccinelle/free.cocci b/tools/coccinelle/free.cocci index 3dfaae9dd8..f2af140cfb 100644 --- a/tools/coccinelle/free.cocci +++ b/tools/coccinelle/free.cocci @@ -6,6 +6,8 @@ expression E; free(E); | commit_list_free(E); +| + FREE_AND_NULL(E); ) @@