From: Ilya Shipitsin Date: Sat, 24 Aug 2024 13:55:44 +0000 (+0200) Subject: DEV: coccinelle: add a test to detect unchecked calloc() X-Git-Tag: v3.1-dev7~48 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8f2112a04f3f253e56f27b611d044eeab2cd5dae;p=thirdparty%2Fhaproxy.git DEV: coccinelle: add a test to detect unchecked calloc() The coccinelle test "unchecked-calloc.cocci" detects various cases of unchecked calloc(). --- diff --git a/dev/coccinelle/unchecked-calloc.cocci b/dev/coccinelle/unchecked-calloc.cocci new file mode 100644 index 0000000000..5433bf0a0e --- /dev/null +++ b/dev/coccinelle/unchecked-calloc.cocci @@ -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;