From 8f2112a04f3f253e56f27b611d044eeab2cd5dae Mon Sep 17 00:00:00 2001 From: Ilya Shipitsin Date: Sat, 24 Aug 2024 15:55:44 +0200 Subject: [PATCH] DEV: coccinelle: add a test to detect unchecked calloc() The coccinelle test "unchecked-calloc.cocci" detects various cases of unchecked calloc(). --- dev/coccinelle/unchecked-calloc.cocci | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 dev/coccinelle/unchecked-calloc.cocci diff --git a/dev/coccinelle/unchecked-calloc.cocci b/dev/coccinelle/unchecked-calloc.cocci new file mode 100644 index 000000000..5433bf0a0 --- /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;