* [PATCH v2 2/2] coccinelle: Add rules to find str_alloc_free() replacements
2026-04-30 6:40 [PATCH v2 0/2] Add str_alloc_free()/str_free_alloc helper and Jiazi Li
2026-04-30 6:40 ` [PATCH v2 1/2] lib/string_choices: Add str_alloc_free() helper Jiazi Li
@ 2026-04-30 6:40 ` Jiazi Li
2026-05-01 8:53 ` Andy Shevchenko
2026-05-01 8:54 ` [PATCH v2 0/2] Add str_alloc_free()/str_free_alloc helper and Andy Shevchenko
2 siblings, 1 reply; 6+ messages in thread
From: Jiazi Li @ 2026-04-30 6:40 UTC (permalink / raw)
To: Kees Cook, Andy Shevchenko, Julia Lawall, Nicolas Palix
Cc: Jiazi Li, linux-hardening, mingzhu.wang
Add rules for finding places where str_alloc_free()/str_free_alloc
can be used.
This currently finds 4 locations.
Adjust the position of some coccinelle rules, so they sorted in the
same order as they appear in the header.
Suggested-by: Andy Shevchenko <andy@kernel.org>
Tested-by: mingzhu.wang <mingzhu.wang@transsion.com>
Signed-off-by: Jiazi Li <jqqlijiazi@gmail.com>
---
V2: Keep coccinelle stances sorted in the same order as they appear in
the header.
---
scripts/coccinelle/api/string_choices.cocci | 228 ++++++++++++--------
1 file changed, 133 insertions(+), 95 deletions(-)
diff --git a/scripts/coccinelle/api/string_choices.cocci b/scripts/coccinelle/api/string_choices.cocci
index 375045086912..516b30828380 100644
--- a/scripts/coccinelle/api/string_choices.cocci
+++ b/scripts/coccinelle/api/string_choices.cocci
@@ -7,109 +7,81 @@ virtual patch
virtual context
virtual report
-@str_plural depends on patch@
+@str_alloc_free depends on patch disable neg_if_exp@
expression E;
@@
-(
-- ((E == 1) ? "" : "s")
-+ str_plural(E)
-|
-- ((E > 1) ? "s" : "")
-+ str_plural(E)
-)
+- ((E) ? "alloc" : "free")
++ str_alloc_free(E)
-@str_plural_r depends on !patch@
+@str_alloc_free_r depends on !patch disable neg_if_exp@
expression E;
position P;
@@
-(
-* (E@P == 1) ? "" : "s"
-|
-* (E@P > 1) ? "s" : ""
-)
+* E@P ? "alloc" : "free"
@script:python depends on report@
-p << str_plural_r.P;
-e << str_plural_r.E;
+p << str_alloc_free_r.P;
+e << str_alloc_free_r.E;
@@
-coccilib.report.print_report(p[0], "opportunity for str_plural(%s)" % e)
+coccilib.report.print_report(p[0], "opportunity for str_alloc_free(%s)" % e)
-@str_up_down depends on patch disable neg_if_exp@
+@str_free_alloc depends on patch disable neg_if_exp@
expression E;
@@
-- ((E) ? "up" : "down")
-+ str_up_down(E)
+- ((E) ? "free" : "alloc")
++ str_free_alloc(E)
-@str_up_down_r depends on !patch disable neg_if_exp@
+@str_free_alloc_r depends on !patch disable neg_if_exp@
expression E;
position P;
@@
-* E@P ? "up" : "down"
+* E@P ? "free" : "alloc"
@script:python depends on report@
-p << str_up_down_r.P;
-e << str_up_down_r.E;
+p << str_free_alloc_r.P;
+e << str_free_alloc_r.E;
@@
-coccilib.report.print_report(p[0], "opportunity for str_up_down(%s)" % e)
+coccilib.report.print_report(p[0], "opportunity for str_free_alloc(%s)" % e)
-@str_down_up depends on patch disable neg_if_exp@
-expression E;
-@@
-- ((E) ? "down" : "up")
-+ str_down_up(E)
-
-@str_down_up_r depends on !patch disable neg_if_exp@
-expression E;
-position P;
-@@
-* E@P ? "down" : "up"
-
-@script:python depends on report@
-p << str_down_up_r.P;
-e << str_down_up_r.E;
-@@
-
-coccilib.report.print_report(p[0], "opportunity for str_down_up(%s)" % e)
-
-@str_true_false depends on patch disable neg_if_exp@
+@str_enable_disable depends on patch@
expression E;
@@
-- ((E) ? "true" : "false")
-+ str_true_false(E)
+- ((E) ? "enable" : "disable")
++ str_enable_disable(E)
-@str_true_false_r depends on !patch disable neg_if_exp@
+@str_enable_disable_r depends on !patch@
expression E;
position P;
@@
-* E@P ? "true" : "false"
+* E@P ? "enable" : "disable"
@script:python depends on report@
-p << str_true_false_r.P;
-e << str_true_false_r.E;
+p << str_enable_disable_r.P;
+e << str_enable_disable_r.E;
@@
-coccilib.report.print_report(p[0], "opportunity for str_true_false(%s)" % e)
+coccilib.report.print_report(p[0], "opportunity for str_enable_disable(%s)" % e)
-@str_false_true depends on patch disable neg_if_exp@
+@str_enabled_disabled depends on patch@
expression E;
@@
-- ((E) ? "false" : "true")
-+ str_false_true(E)
+- ((E) ? "enabled" : "disabled")
++ str_enabled_disabled(E)
-@str_false_true_r depends on !patch disable neg_if_exp@
+@str_enabled_disabled_r depends on !patch@
expression E;
position P;
@@
-* E@P ? "false" : "true"
+* E@P ? "enabled" : "disabled"
@script:python depends on report@
-p << str_false_true_r.P;
-e << str_false_true_r.E;
+p << str_enabled_disabled_r.P;
+e << str_enabled_disabled_r.E;
@@
-coccilib.report.print_report(p[0], "opportunity for str_false_true(%s)" % e)
+coccilib.report.print_report(p[0], "opportunity for str_enabled_disabled(%s)" % e)
@str_hi_lo depends on patch disable neg_if_exp@
expression E;
@@ -187,43 +159,24 @@ e << str_low_high_r.E;
coccilib.report.print_report(p[0], "opportunity for str_low_high(%s)" % e)
-@str_enable_disable depends on patch@
-expression E;
-@@
-- ((E) ? "enable" : "disable")
-+ str_enable_disable(E)
-
-@str_enable_disable_r depends on !patch@
-expression E;
-position P;
-@@
-* E@P ? "enable" : "disable"
-
-@script:python depends on report@
-p << str_enable_disable_r.P;
-e << str_enable_disable_r.E;
-@@
-
-coccilib.report.print_report(p[0], "opportunity for str_enable_disable(%s)" % e)
-
-@str_enabled_disabled depends on patch@
+@str_on_off depends on patch@
expression E;
@@
-- ((E) ? "enabled" : "disabled")
-+ str_enabled_disabled(E)
+- ((E) ? "on" : "off")
++ str_on_off(E)
-@str_enabled_disabled_r depends on !patch@
+@str_on_off_r depends on !patch@
expression E;
position P;
@@
-* E@P ? "enabled" : "disabled"
+* E@P ? "on" : "off"
@script:python depends on report@
-p << str_enabled_disabled_r.P;
-e << str_enabled_disabled_r.E;
+p << str_on_off_r.P;
+e << str_on_off_r.E;
@@
-coccilib.report.print_report(p[0], "opportunity for str_enabled_disabled(%s)" % e)
+coccilib.report.print_report(p[0], "opportunity for str_on_off(%s)" % e)
@str_read_write depends on patch disable neg_if_exp@
expression E;
@@ -263,24 +216,81 @@ e << str_write_read_r.E;
coccilib.report.print_report(p[0], "opportunity for str_write_read(%s)" % e)
-@str_on_off depends on patch@
+@str_true_false depends on patch disable neg_if_exp@
expression E;
@@
-- ((E) ? "on" : "off")
-+ str_on_off(E)
+- ((E) ? "true" : "false")
++ str_true_false(E)
-@str_on_off_r depends on !patch@
+@str_true_false_r depends on !patch disable neg_if_exp@
expression E;
position P;
@@
-* E@P ? "on" : "off"
+* E@P ? "true" : "false"
@script:python depends on report@
-p << str_on_off_r.P;
-e << str_on_off_r.E;
+p << str_true_false_r.P;
+e << str_true_false_r.E;
@@
-coccilib.report.print_report(p[0], "opportunity for str_on_off(%s)" % e)
+coccilib.report.print_report(p[0], "opportunity for str_true_false(%s)" % e)
+
+@str_false_true depends on patch disable neg_if_exp@
+expression E;
+@@
+- ((E) ? "false" : "true")
++ str_false_true(E)
+
+@str_false_true_r depends on !patch disable neg_if_exp@
+expression E;
+position P;
+@@
+* E@P ? "false" : "true"
+
+@script:python depends on report@
+p << str_false_true_r.P;
+e << str_false_true_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_false_true(%s)" % e)
+
+@str_up_down depends on patch disable neg_if_exp@
+expression E;
+@@
+- ((E) ? "up" : "down")
++ str_up_down(E)
+
+@str_up_down_r depends on !patch disable neg_if_exp@
+expression E;
+position P;
+@@
+* E@P ? "up" : "down"
+
+@script:python depends on report@
+p << str_up_down_r.P;
+e << str_up_down_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_up_down(%s)" % e)
+
+@str_down_up depends on patch disable neg_if_exp@
+expression E;
+@@
+- ((E) ? "down" : "up")
++ str_down_up(E)
+
+@str_down_up_r depends on !patch disable neg_if_exp@
+expression E;
+position P;
+@@
+* E@P ? "down" : "up"
+
+@script:python depends on report@
+p << str_down_up_r.P;
+e << str_down_up_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_down_up(%s)" % e)
@str_yes_no depends on patch@
expression E;
@@ -300,3 +310,31 @@ e << str_yes_no_r.E;
@@
coccilib.report.print_report(p[0], "opportunity for str_yes_no(%s)" % e)
+
+@str_plural depends on patch@
+expression E;
+@@
+(
+- ((E == 1) ? "" : "s")
++ str_plural(E)
+|
+- ((E > 1) ? "s" : "")
++ str_plural(E)
+)
+
+@str_plural_r depends on !patch@
+expression E;
+position P;
+@@
+(
+* (E@P == 1) ? "" : "s"
+|
+* (E@P > 1) ? "s" : ""
+)
+
+@script:python depends on report@
+p << str_plural_r.P;
+e << str_plural_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_plural(%s)" % e)
--
2.49.0
^ permalink raw reply related [flat|nested] 6+ messages in thread