The coccinelle test "unchecked-calloc.cocci" detects various cases of
unchecked calloc().
---
 dev/coccinelle/unchecked-calloc.cocci | 34 +++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 dev/coccinelle/unchecked-calloc.cocci

diff --git a/dev/coccinelle/unchecked-calloc.cocci 
b/dev/coccinelle/unchecked-calloc.cocci
new file mode 100644
index 000000000..5433bf0a0
--- /dev/null
+++ b/dev/coccinelle/unchecked-calloc.cocci
@@ -0,0 +1,34 @@
+// find calls to calloc
+@call@
+expression ptr;
+position p;
+@@
+
+ptr@p = calloc(...);
+
+// find ok calls to calloc
+@ok@
+expression ptr;
+position call.p;
+@@
+
+ptr@p = calloc(...);
+... when != ptr
+(
+ (ptr == NULL || ...)
+|
+ (ptr == 0 || ...)
+|
+ (ptr != NULL || ...)
+|
+ (ptr != 0 || ...)
+)
+
+// fix bad calls to calloc
+@depends on !ok@
+expression ptr;
+position call.p;
+@@
+
+ptr@p = calloc(...);
++ if (ptr == NULL) return;
-- 
2.46.0



Reply via email to