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

[PATCH v3 2/4] cocci: catch unused "strbuf" using an xmalloc() pattern

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jul 1, 2022, 10:30 UTC
Message-ID
<patch-v3-2.4-6324d3956ed-20220701T102506Z-avarab@gmail.com>
In-Reply-To
<cover-v3-0.4-00000000000-20220701T102506Z-avarab@gmail.com>
There's no current matches for this rule, but it will match both:
	struct strbuf *buf = xmalloc(sizeof(struct strbuf));
	strbuf_init(buf, 0);
	strbuf_release(buf);
And:
	struct strbuf *buf;
	buf = xmalloc(sizeof(struct strbuf));
	strbuf_init(buf, 0);
	strbuf_release(buf);

Note that we'd also match a strbuf_init() before the xmalloc(), but we're not seeking to be so strict as to make checks that the compiler will catch for us redundant, and saying we'll match either "init" or "xmalloc" lines makes the rule simpler.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 contrib/coccinelle/unused.cocci | 12 ++++++++++++
 1 file changed, 12 insertions(+)
diff --git a/contrib/coccinelle/unused.cocci b/contrib/coccinelle/unused.cocci
index bc26d39b313..43942f3cd4f 100644
--- a/contrib/coccinelle/unused.cocci
+++ b/contrib/coccinelle/unused.cocci
@@ -22,6 +22,8 @@ type T;
 identifier I;
 // STRBUF_INIT
 constant INIT_MACRO =~ "^STRBUF_INIT$";
+// x[mc]alloc() etc.
+identifier MALLOC1 =~ "^x?[mc]alloc$";
 // strbuf_init(&I, ...) etc.
 identifier INIT_CALL1 =~ "^strbuf_init$";
 // strbuf_release()
@@ -37,15 +39,25 @@ identifier REL1 =~ "^strbuf_release$";
 // ... or "struct STRBUF buf = STRBUF_INIT;" ...
 |
 - T I = INIT_MACRO;
+|
+// ... or "struct strbuf *buf = xmalloc(...)" etc. ...
+- T I = MALLOC1(...);
 )
 
 // ... Optionally followed by lines that make no use of "buf", "&buf"
 // etc., but which ...
 <... when != \( I \| &I \)
      when strict
+(
 // .. (only) make use of "buf" or "&buf" to call something like
 // "strbuf_init(&buf, ...)" ...
 - \( INIT_CALL1 \)( \( I \| &I \), ...);
+|
+// .. or to follow-up a "struct strbuf *buf" with e.g. "buf =
+// xmalloc(...)" (which may in turn be followed-up by a
+// "strbuf_init()", which we'll match with INIT_CALL1) ...
+- I = MALLOC1(...);
+)
 ...>
 
 // ... and then no mention of "buf" or "&buf" until we get to a
-- 
2.37.0.900.g4d0de1cceb2
Previous: Eric SunshineNext: Ævar Arnfjörð Bjarmason
Message 10 of 24 in “cocci: add and apply a rule to find "unused" variables”
  1. cocci: add and apply a rule to find "unused" variablesÆvar Arnfjörð Bjarmason, May 20, 2022
  2. 0/2 add and apply a rule to find "unused" init+freeÆvar Arnfjörð Bjarmason, Jun 21, 2022
  3. 1/2 cocci: add and apply a rule to find "unused" variablesÆvar Arnfjörð Bjarmason, Jun 21, 2022
  4. Junio C HamanoJun 22, 2022
  5. 2/2 cocci: remove "when strict" from unused.cocciÆvar Arnfjörð Bjarmason, Jun 21, 2022
  6. 0/4 add and apply a rule to find "unused" init+freeÆvar Arnfjörð Bjarmason, Jul 1, 2022
  7. 1/4 cocci: add and apply a rule to find "unused" strbufsÆvar Arnfjörð Bjarmason, Jul 1, 2022
  8. Jeff KingJul 1, 2022
  9. Eric SunshineJul 1, 2022
  10. 2/4 cocci: catch unused "strbuf" using an xmalloc() patternÆvar Arnfjörð Bjarmason, Jul 1, 2022
  11. 4/4 cocci: generalize "unused" rule to cover more than "strbuf"Ævar Arnfjörð Bjarmason, Jul 1, 2022
  12. 3/4 cocci: remove "when strict" from unused.cocciÆvar Arnfjörð Bjarmason, Jul 1, 2022
  13. Eric SunshineJul 1, 2022
  14. Jeff KingJul 1, 2022
  15. 0/6 add and apply a rule to find "unused" init+freeÆvar Arnfjörð Bjarmason, Jul 5, 2022
  16. 2/6 Makefile & .gitignore: ignore & clean "git.res", not "*.res"Ævar Arnfjörð Bjarmason, Jul 5, 2022
  17. 1/6 Makefile: remove mandatory "spatch" arguments from SPATCH_FLAGSÆvar Arnfjörð Bjarmason, Jul 5, 2022
  18. 3/6 cocci: add a "coccicheck-test" target and test *.cocci rulesÆvar Arnfjörð Bjarmason, Jul 5, 2022
  19. 4/6 cocci: have "coccicheck{,-pending}" depend on "coccicheck-test"Ævar Arnfjörð Bjarmason, Jul 5, 2022
  20. 5/6 cocci: add and apply a rule to find "unused" strbufsÆvar Arnfjörð Bjarmason, Jul 5, 2022
  21. 6/6 cocci: generalize "unused" rule to cover more than "strbuf"Ævar Arnfjörð Bjarmason, Jul 5, 2022
  22. Junio C HamanoJul 6, 2022
  23. Jeff KingJul 11, 2022
  24. Ævar Arnfjörð BjarmasonJul 11, 2022

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.