The coccinelle test "unchecked-malloc.cocci" detects various cases of unchecked malloc(). --- dev/coccinelle/unchecked-malloc.cocci | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 dev/coccinelle/unchecked-malloc.cocci
diff --git a/dev/coccinelle/unchecked-malloc.cocci b/dev/coccinelle/unchecked-malloc.cocci new file mode 100644 index 000000000..30b2bf88a --- /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; -- 2.46.0