Why is this an issue?
UNIX/POSIX functions can have undefined behavior if they are not called correctly. More specifically:
- allocation size of
calloc
, malloc
, realloc
, reallocf
, alloca
and
valloc
should be strictly positive
-
open
and openat
should be called with a flag that contains one access mode: O_RDONLY
,
O_WRONLY
, or O_RDWR
-
open
and openat
with flag O_CREAT
should be called with a third argument
- flag
O_EXCL
should be used with O_CREAT
- first argument of
pthread_once
should not have automatic storage duration and should be initialized by
PTHREAD_ONCE_INIT
Noncompliant code example
int res = open(file, O_CREAT); // Noncompliant, flag O_CREAT requires a third argument
void *mem = alloca(0); // Noncompliant, allocation of 0 bytes
extern void initialize();
pthread_once_t pthread = PTHREAD_ONCE_INIT;
pthread_once(&pthread, initialize); // Noncompliant, do not pass a local variable as argument