From: Ilya Shipitsin Date: Sat, 24 Aug 2024 13:55:43 +0000 (+0200) Subject: DEV: coccinelle: add a test to detect unchecked malloc() X-Git-Tag: v3.1-dev7~49 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2ec42bff48581a36cdb94bbcf190c68cfa80cbfd;p=thirdparty%2Fhaproxy.git DEV: coccinelle: add a test to detect unchecked malloc() The coccinelle test "unchecked-malloc.cocci" detects various cases of unchecked malloc(). --- diff --git a/dev/coccinelle/unchecked-malloc.cocci b/dev/coccinelle/unchecked-malloc.cocci new file mode 100644 index 0000000000..30b2bf88ab --- /dev/null +++ b/dev/coccinelle/unchecked-malloc.cocci @@ -0,0 +1,34 @@ +// find calls to malloc +@call@ +expression ptr; +position p; +@@ + +ptr@p = malloc(...); + +// find ok calls to malloc +@ok@ +expression ptr; +position call.p; +@@ + +ptr@p = malloc(...); +... when != ptr +( + (ptr == NULL || ...) +| + (ptr == 0 || ...) +| + (ptr != NULL || ...) +| + (ptr != 0 || ...) +) + +// fix bad calls to malloc +@depends on !ok@ +expression ptr; +position call.p; +@@ + +ptr@p = malloc(...); ++ if (ptr == NULL) return;