]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
DEV: coccinelle: add a test to detect unchecked calloc()
authorIlya Shipitsin <chipitsine@gmail.com>
Sat, 24 Aug 2024 13:55:44 +0000 (15:55 +0200)
committerWilly Tarreau <w@1wt.eu>
Sat, 24 Aug 2024 17:13:56 +0000 (19:13 +0200)
The coccinelle test "unchecked-calloc.cocci" detects various cases of
unchecked calloc().

dev/coccinelle/unchecked-calloc.cocci [new file with mode: 0644]

diff --git a/dev/coccinelle/unchecked-calloc.cocci b/dev/coccinelle/unchecked-calloc.cocci
new file mode 100644 (file)
index 0000000..5433bf0
--- /dev/null
@@ -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;