xref: /llvm-project/clang/docs/analyzer/checkers/unix_api_example.c (revision 1a17032b788016299ea4e3c4b53670c6dcd94b4f)
1*1a17032bSKristof Umann 
2*1a17032bSKristof Umann // Currently the check is performed for apple targets only.
test(const char * path)3*1a17032bSKristof Umann void test(const char *path) {
4*1a17032bSKristof Umann   int fd = open(path, O_CREAT);
5*1a17032bSKristof Umann     // warn: call to 'open' requires a third argument when the
6*1a17032bSKristof Umann     // 'O_CREAT' flag is set
7*1a17032bSKristof Umann }
8*1a17032bSKristof Umann 
9*1a17032bSKristof Umann void f();
10*1a17032bSKristof Umann 
test()11*1a17032bSKristof Umann void test() {
12*1a17032bSKristof Umann   pthread_once_t pred = {0x30B1BCBA, {0}};
13*1a17032bSKristof Umann   pthread_once(&pred, f);
14*1a17032bSKristof Umann     // warn: call to 'pthread_once' uses the local variable
15*1a17032bSKristof Umann }
16*1a17032bSKristof Umann 
test()17*1a17032bSKristof Umann void test() {
18*1a17032bSKristof Umann   void *p = malloc(0); // warn: allocation size of 0 bytes
19*1a17032bSKristof Umann }
20*1a17032bSKristof Umann 
test()21*1a17032bSKristof Umann void test() {
22*1a17032bSKristof Umann   void *p = calloc(0, 42); // warn: allocation size of 0 bytes
23*1a17032bSKristof Umann }
24*1a17032bSKristof Umann 
test()25*1a17032bSKristof Umann void test() {
26*1a17032bSKristof Umann   void *p = malloc(1);
27*1a17032bSKristof Umann   p = realloc(p, 0); // warn: allocation size of 0 bytes
28*1a17032bSKristof Umann }
29*1a17032bSKristof Umann 
test()30*1a17032bSKristof Umann void test() {
31*1a17032bSKristof Umann   void *p = alloca(0); // warn: allocation size of 0 bytes
32*1a17032bSKristof Umann }
33*1a17032bSKristof Umann 
test()34*1a17032bSKristof Umann void test() {
35*1a17032bSKristof Umann   void *p = valloc(0); // warn: allocation size of 0 bytes
36*1a17032bSKristof Umann }
37*1a17032bSKristof Umann 
38