]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
DEV: coccinelle: add a test to detect unchecked strdup()
authorIlia Shipitsin <chipitsine@gmail.com>
Mon, 5 Aug 2024 18:59:09 +0000 (20:59 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 6 Aug 2024 06:21:49 +0000 (08:21 +0200)
The coccinelle test "unchecked-strdup.cocci" detects various cases of
unchecked strdup().

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

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