Add str_alloc_free()/str_free_alloc helper and related rules
Subsystems (2)
cover Add str_alloc_free()/str_free_alloc helper and related rules
Currently finds 4 locations:
./drivers/net/wireless/realtek/rtw89/fw.c:2557:7-12: opportunity for str_alloc_free(valid) ./drivers/net/wireless/realtek/rtw89/fw.c:2693:7-12: opportunity for str_alloc_free(valid) ./drivers/android/tests/binder_alloc_kunit.c:196:6-21: opportunity for str_alloc_free(alloc -> pages [ i ]) ./mm/slub.c:1634:3-8: opportunity for str_alloc_free(alloc)
Jiazi Li (2):
lib/string_choices: Add str_alloc_free() helper
coccinelle: Add rules to find str_alloc_free() replacements
include/linux/string_choices.h | 6 ++++ scripts/coccinelle/api/string_choices.cocci | 38 +++++++++++++++++++++ 2 files changed, 44 insertions(+)
Signature · 1 line
2.49.0
PATCH 1/2 lib/string_choices: Add str_alloc_free() helper
Add str_alloc_free() helper to return "alloc" or "free" string literal depending on the boolean argument. Also add the inversed variant str_free_alloc().
--- include/linux/string_choices.h | 6 ++++++ 1 file changed, 6 insertions(+)
include/linux/string_choices.h +6 -0
@@ -83,6 +83,12 @@ static inline const char *str_yes_no(bool v) }#define str_no_yes (v) str_yes_no(!(v)) +static inline const char *str_alloc_free (bool v) +{ +return v ?"alloc" :"free" ; +} +#define str_free_alloc (v) str_alloc_free(!(v)) +/** * str_plural - Return the simple pluralization based on English counts * @num: Number used for deciding pluralization
Signature · 1 line
2.49.0
Re: [PATCH 1/2] lib/string_choices: Add str_alloc_free() helper
Quoted 4 lines · Jiazi Li
Add
str_alloc_free()helper to return "alloc" or "free" string literal depending on the boolean argument. Also add the inversed variantstr_free_alloc().
...
include/linux/string_choices.h static inline const char *str_yes_no(bool v) · line 90
Quoted 7 lines
#define
str_no_yes(v)str_yes_no(!(v))include/linux/string_choices.h +5
-0+static inline const char *str_alloc_free (bool v) +{ +return v ?"alloc" :"free" ; +} +#define str_free_alloc (v) str_alloc_free(!(v))
The appearance of the function is sorted alphabetically.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH 2/2 coccinelle: Add rules to find str_alloc_free() replacements
In reply to Jiazi LiAdd rules for finding places where str_alloc_free()/str_free_alloc can be used.
--- scripts/coccinelle/api/string_choices.cocci | 38 +++++++++++++++++++++ 1 file changed, 38 insertions(+)
scripts/coccinelle/api/string_choices.cocci +38 -0
@@ -300,3 +300,41 @@ e << str_yes_no_r.E; @@ coccilib.report.print_report(p[0], "opportunity for str_yes_no(%s)" % e) + +@str_alloc_free depends on patch disable neg_if_exp@ +expression E; +@@ +- ((E) ? "alloc" : "free") ++ str_alloc_free(E) + +@str_alloc_free_r depends on !patch disable neg_if_exp@ +expression E; +position P; +@@ +* E@P ? "alloc" : "free" + +@script:python depends on report@ +p << str_alloc_free_r.P; +e << str_alloc_free_r.E; +@@ + +coccilib.report.print_report(p[0], "opportunity for str_alloc_free(%s)" % e) + +@str_free_alloc depends on patch disable neg_if_exp@ +expression E; +@@ +- ((E) ? "free" : "alloc") ++ str_free_alloc(E) + +@str_free_alloc_r depends on !patch disable neg_if_exp@ +expression E; +position P; +@@ +* E@P ? "free" : "alloc" + +@script:python depends on report@ +p << str_free_alloc_r.P; +e << str_free_alloc_r.E; +@@ + +coccilib.report.print_report(p[0], "opportunity for str_free_alloc(%s)" % e)
Signature · 1 line
2.49.0
Re: [PATCH 2/2] coccinelle: Add rules to find str_alloc_free() replacements
Quoted 2 lines · Jiazi Li
Add rules for finding places where str_alloc_free()/str_free_alloc
str_free_alloc()
Quoted 1 line
can be used.
...
Quoted 3 lines
coccilib.report.print_report(p[0], "opportunity for str_yes_no(%s)" % e)+coccilib.report.print_report(p[0], "opportunity for
str_alloc_free(%s)" % e)
Perhaps also makes sense to keep coccinelle stances sorted in the same order as they appear in the header.
Signature · 2 lines
With Best Regards, Andy Shevchenko