From 661e1db826cc3dd99c5acc5ad768bb7e28fe59d3 Mon Sep 17 00:00:00 2001 From: Ilia Shipitsin Date: Mon, 5 Aug 2024 20:59:09 +0200 Subject: [PATCH] DEV: coccinelle: add a test to detect unchecked strdup() The coccinelle test "unchecked-strdup.cocci" detects various cases of unchecked strdup(). --- dev/coccinelle/unchecked-strdup.cocci | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 dev/coccinelle/unchecked-strdup.cocci diff --git a/dev/coccinelle/unchecked-strdup.cocci b/dev/coccinelle/unchecked-strdup.cocci new file mode 100644 index 000000000..fa109aaf1 --- /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;