From: Ilia Shipitsin Date: Mon, 5 Aug 2024 18:59:09 +0000 (+0200) Subject: DEV: coccinelle: add a test to detect unchecked strdup() X-Git-Tag: v3.1-dev5~44 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=661e1db826cc3dd99c5acc5ad768bb7e28fe59d3;p=thirdparty%2Fhaproxy.git DEV: coccinelle: add a test to detect unchecked strdup() The coccinelle test "unchecked-strdup.cocci" detects various cases of unchecked strdup(). --- diff --git a/dev/coccinelle/unchecked-strdup.cocci b/dev/coccinelle/unchecked-strdup.cocci new file mode 100644 index 0000000000..fa109aaf1a --- /dev/null +++ b/dev/coccinelle/unchecked-strdup.cocci @@ -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;