This patch adds a trivial script warning on

if(foo)
        kfree(foo)

(based on checkpatch)

Cc: Julia Lawall <julia.law...@lip6.fr>
Cc: Gilles Muller <gilles.mul...@lip6.fr>
Cc: Joe Perches <j...@perches.com>
Signed-off-by: Fabian Frederick <f...@skynet.be>
---
 scripts/coccinelle/free/cond_kfree.cocci | 34 ++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 scripts/coccinelle/free/cond_kfree.cocci

diff --git a/scripts/coccinelle/free/cond_kfree.cocci 
b/scripts/coccinelle/free/cond_kfree.cocci
new file mode 100644
index 0000000..0b8093e
--- /dev/null
+++ b/scripts/coccinelle/free/cond_kfree.cocci
@@ -0,0 +1,34 @@
+/// conditional kfree - NULL check before kfree is not needed.
+///
+/// Based on checkpatch warning
+/// "kfree(NULL) is safe this check is probably not required"
+/// and kfreeaddr.cocci by Julia Lawall.
+///
+/// Comments: -
+/// Options: --no-includes --include-headers
+
+virtual org
+virtual report
+virtual context
+
+@r depends on context || report || org @
+expression E;
+position p;
+@@
+
+* if (E)
+*      kfree@p(E);
+
+@script:python depends on org@
+p << r.p;
+@@
+
+cocci.print_main("kfree", p)
+
+@script:python depends on report@
+p << r.p;
+@@
+
+msg = "WARNING: checking value to avoid kfree(NULL) is unnecessary."
+coccilib.report.print_report(p[0], msg)
+
-- 
1.8.4.5

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to