haproxy/dev/coccinelle/unchecked-malloc.cocci
Ilya Shipitsin 2ec42bff48 DEV: coccinelle: add a test to detect unchecked malloc()
The coccinelle test "unchecked-malloc.cocci" detects various cases of
unchecked malloc().
2024-08-24 19:13:56 +02:00

35 lines
413 B
Plaintext

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