DEV: coccinelle: add a test to detect unchecked malloc()

The coccinelle test "unchecked-malloc.cocci" detects various cases of
unchecked malloc().
This commit is contained in:
Ilya Shipitsin 2024-08-24 15:55:43 +02:00 committed by Willy Tarreau
parent 7a03ab426f
commit 2ec42bff48

View File

@ -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;