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

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

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