* [PATCH] coccinelle: check for casting malloc output
@ 2020-04-19 9:07 Heinrich Schuchardt
2020-04-26 11:27 ` Tom Rini
0 siblings, 1 reply; 2+ messages in thread
From: Heinrich Schuchardt @ 2020-04-19 9:07 UTC (permalink / raw)
To: u-boot
Casting the (void *) output of memory allocation functions before
assignment like in
sata->cmd_hdr_tbl_offset = (void *)malloc(length + align);
is useless.
Adopt the Linux kernel script
scripts/coccinelle/api/alloc/alloc_cast.cocci.
Now 'make coccicheck' generates warnings like:
./drivers/ata/fsl_sata.c:143:29-33:
WARNING: casting value returned by memory allocation function
to (void *) is useless.
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
scripts/coccinelle/api/alloc/alloc_cast.cocci | 102 ++++++++++++++++++
1 file changed, 102 insertions(+)
create mode 100644 scripts/coccinelle/api/alloc/alloc_cast.cocci
diff --git a/scripts/coccinelle/api/alloc/alloc_cast.cocci b/scripts/coccinelle/api/alloc/alloc_cast.cocci
new file mode 100644
index 0000000000..e336784a9f
--- /dev/null
+++ b/scripts/coccinelle/api/alloc/alloc_cast.cocci
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// Remove casting the values returned by memory allocation functions
+/// like kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc.
+///
+//# This makes an effort to find cases of casting of values returned by
+//# malloc, calloc, kmalloc, kmalloc_array, kmalloc_node and removes
+//# the casting as it is not required. The result in the patch case may
+//# need some reformatting.
+//
+// Confidence: High
+// Copyright: (C) 2014 Himangi Saraogi
+// Copyright: (C) 2017 Himanshu Jha
+// Comments:
+// Options: --no-includes --include-headers
+//
+
+virtual context
+virtual patch
+virtual org
+virtual report
+
+ at initialize:python@
+@@
+import re
+pattern = '__'
+m = re.compile(pattern)
+
+ at r1 depends on context || patch@
+type T;
+@@
+
+ (T *)
+ \(malloc\|calloc\|kmalloc\|kmalloc_array\|kmalloc_node\)(...)
+
+//----------------------------------------------------------
+// For context mode
+//----------------------------------------------------------
+
+ at script:python depends on context@
+t << r1.T;
+@@
+
+if m.search(t) != None:
+ cocci.include_match(False)
+
+ at depends on context && r1@
+type r1.T;
+@@
+
+* (T *)
+ \(malloc\|calloc\|kmalloc\|kmalloc_array\|kmalloc_node\)(...)
+
+//----------------------------------------------------------
+// For patch mode
+//----------------------------------------------------------
+
+ at script:python depends on patch@
+t << r1.T;
+@@
+
+if m.search(t) != None:
+ cocci.include_match(False)
+
+ at depends on patch && r1@
+type r1.T;
+@@
+
+- (T *)
+ \(malloc\|calloc\|kmalloc\|kmalloc_array\|kmalloc_node\)(...)
+
+//----------------------------------------------------------
+// For org and report mode
+//----------------------------------------------------------
+
+ at r2 depends on org || report@
+type T;
+position p;
+@@
+
+ (T at p *)
+ \(malloc\|calloc\|kmalloc\|kmalloc_array\|kmalloc_node\)(...)
+
+ at script:python depends on org@
+p << r2.p;
+t << r2.T;
+@@
+
+if m.search(t) != None:
+ cocci.include_match(False)
+else:
+ coccilib.org.print_safe_todo(p[0], t)
+
+@script:python depends on report@
+p << r2.p;
+t << r2.T;
+@@
+
+if m.search(t) != None:
+ cocci.include_match(False)
+else:
+ msg="WARNING: casting value returned by memory allocation function to (%s *) is useless." % (t)
+ coccilib.report.print_report(p[0], msg)
--
2.25.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH] coccinelle: check for casting malloc output
2020-04-19 9:07 [PATCH] coccinelle: check for casting malloc output Heinrich Schuchardt
@ 2020-04-26 11:27 ` Tom Rini
0 siblings, 0 replies; 2+ messages in thread
From: Tom Rini @ 2020-04-26 11:27 UTC (permalink / raw)
To: u-boot
On Sun, Apr 19, 2020 at 11:07:34AM +0200, Heinrich Schuchardt wrote:
> Casting the (void *) output of memory allocation functions before
> assignment like in
>
> sata->cmd_hdr_tbl_offset = (void *)malloc(length + align);
>
> is useless.
>
> Adopt the Linux kernel script
> scripts/coccinelle/api/alloc/alloc_cast.cocci.
>
> Now 'make coccicheck' generates warnings like:
>
> ./drivers/ata/fsl_sata.c:143:29-33:
> WARNING: casting value returned by memory allocation function
> to (void *) is useless.
>
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Applied to u-boot/master, thanks!
--
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200426/2afcafab/attachment.sig>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-04-26 11:27 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-19 9:07 [PATCH] coccinelle: check for casting malloc output Heinrich Schuchardt
2020-04-26 11:27 ` Tom Rini
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox