• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=unix.API,osx.API %s -analyzer-store=region -fblocks -verify
2 
3 struct _opaque_pthread_once_t {
4   long __sig;
5   char __opaque[8];
6 };
7 typedef struct _opaque_pthread_once_t    __darwin_pthread_once_t;
8 typedef __darwin_pthread_once_t pthread_once_t;
9 int pthread_once(pthread_once_t *, void (*)(void));
10 typedef long unsigned int __darwin_size_t;
11 typedef __darwin_size_t size_t;
12 void *calloc(size_t, size_t);
13 void *malloc(size_t);
14 void *realloc(void *, size_t);
15 void *alloca(size_t);
16 void *valloc(size_t);
17 
18 typedef void (^dispatch_block_t)(void);
19 typedef long dispatch_once_t;
20 void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);
21 
22 #ifndef O_CREAT
23 #define O_CREAT 0x0200
24 #define O_RDONLY 0x0000
25 #endif
26 int open(const char *, int, ...);
27 int close(int fildes);
28 
test_open(const char * path)29 void test_open(const char *path) {
30   int fd;
31   fd = open(path, O_RDONLY); // no-warning
32   if (!fd)
33     close(fd);
34 
35   fd = open(path, O_CREAT); // expected-warning{{Call to 'open' requires a third argument when the 'O_CREAT' flag is set}}
36   if (!fd)
37     close(fd);
38 }
39 
test_dispatch_once()40 void test_dispatch_once() {
41   dispatch_once_t pred = 0;
42   do { if (__builtin_expect(*(&pred), ~0l) != ~0l) dispatch_once((&pred), (^() {})); } while (0); // expected-warning{{Call to 'dispatch_once' uses the local variable 'pred' for the predicate value}}
43 }
test_dispatch_once_neg()44 void test_dispatch_once_neg() {
45   static dispatch_once_t pred = 0;
46   do { if (__builtin_expect(*(&pred), ~0l) != ~0l) dispatch_once((&pred), (^() {})); } while (0); // no-warning
47 }
48 
49 void test_pthread_once_aux();
50 
test_pthread_once()51 void test_pthread_once() {
52   pthread_once_t pred = {0x30B1BCBA, {0}};
53   pthread_once(&pred, test_pthread_once_aux); // expected-warning{{Call to 'pthread_once' uses the local variable 'pred' for the "control" value}}
54 }
test_pthread_once_neg()55 void test_pthread_once_neg() {
56   static pthread_once_t pred = {0x30B1BCBA, {0}};
57   pthread_once(&pred, test_pthread_once_aux); // no-warning
58 }
59 
60 // PR 2899 - warn of zero-sized allocations to malloc().
pr2899()61 void pr2899() {
62   char* foo = malloc(0); // expected-warning{{Call to 'malloc' has an allocation size of 0 bytes}}
63   for (unsigned i = 0; i < 100; i++) {
64     foo[i] = 0;
65   }
66 }
pr2899_nowarn(size_t size)67 void pr2899_nowarn(size_t size) {
68   char* foo = malloc(size); // no-warning
69   for (unsigned i = 0; i < 100; i++) {
70     foo[i] = 0;
71   }
72 }
test_calloc(void)73 void test_calloc(void) {
74   char *foo = calloc(0, 42); // expected-warning{{Call to 'calloc' has an allocation size of 0 bytes}}
75   for (unsigned i = 0; i < 100; i++) {
76     foo[i] = 0;
77   }
78 }
test_calloc2(void)79 void test_calloc2(void) {
80   char *foo = calloc(42, 0); // expected-warning{{Call to 'calloc' has an allocation size of 0 bytes}}
81   for (unsigned i = 0; i < 100; i++) {
82     foo[i] = 0;
83   }
84 }
test_calloc_nowarn(size_t nmemb,size_t size)85 void test_calloc_nowarn(size_t nmemb, size_t size) {
86   char *foo = calloc(nmemb, size); // no-warning
87   for (unsigned i = 0; i < 100; i++) {
88     foo[i] = 0;
89   }
90 }
test_realloc(char * ptr)91 void test_realloc(char *ptr) {
92   char *foo = realloc(ptr, 0); // expected-warning{{Call to 'realloc' has an allocation size of 0 bytes}}
93   for (unsigned i = 0; i < 100; i++) {
94     foo[i] = 0;
95   }
96 }
test_realloc_nowarn(char * ptr,size_t size)97 void test_realloc_nowarn(char *ptr, size_t size) {
98   char *foo = realloc(ptr, size); // no-warning
99   for (unsigned i = 0; i < 100; i++) {
100     foo[i] = 0;
101   }
102 }
test_alloca()103 void test_alloca() {
104   char *foo = alloca(0); // expected-warning{{Call to 'alloca' has an allocation size of 0 bytes}}
105   for(unsigned i = 0; i < 100; i++) {
106     foo[i] = 0;
107   }
108 }
test_alloca_nowarn(size_t sz)109 void test_alloca_nowarn(size_t sz) {
110   char *foo = alloca(sz); // no-warning
111   for(unsigned i = 0; i < 100; i++) {
112     foo[i] = 0;
113   }
114 }
test_builtin_alloca()115 void test_builtin_alloca() {
116   char *foo2 = __builtin_alloca(0); // expected-warning{{Call to 'alloca' has an allocation size of 0 bytes}}
117   for(unsigned i = 0; i < 100; i++) {
118     foo2[i] = 0;
119   }
120 }
test_builtin_alloca_nowarn(size_t sz)121 void test_builtin_alloca_nowarn(size_t sz) {
122   char *foo2 = __builtin_alloca(sz); // no-warning
123   for(unsigned i = 0; i < 100; i++) {
124     foo2[i] = 0;
125   }
126 }
test_valloc()127 void test_valloc() {
128   char *foo = valloc(0); // expected-warning{{Call to 'valloc' has an allocation size of 0 bytes}}
129   for(unsigned i = 0; i < 100; i++) {
130     foo[i] = 0;
131   }
132 }
test_valloc_nowarn(size_t sz)133 void test_valloc_nowarn(size_t sz) {
134   char *foo = valloc(sz); // no-warning
135   for(unsigned i = 0; i < 100; i++) {
136     foo[i] = 0;
137   }
138 }
139