unix-fns.c revision 3e97758f22f31d0dbc336fc4794b86aed8607053
1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=unix.API,osx.API %s -analyzer-store=region -fblocks -verify
2
3struct _opaque_pthread_once_t {
4  long __sig;
5  char __opaque[8];
6};
7typedef struct _opaque_pthread_once_t    __darwin_pthread_once_t;
8typedef __darwin_pthread_once_t pthread_once_t;
9int pthread_once(pthread_once_t *, void (*)(void));
10typedef long unsigned int __darwin_size_t;
11typedef __darwin_size_t size_t;
12void *calloc(size_t, size_t);
13void *malloc(size_t);
14void *realloc(void *, size_t);
15void *alloca(size_t);
16void *valloc(size_t);
17
18typedef void (^dispatch_block_t)(void);
19typedef long dispatch_once_t;
20void 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
26int open(const char *, int, ...);
27int close(int fildes);
28
29void 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
40void 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}
44void 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
49void test_pthread_once_aux();
50
51void 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}
55void 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().
61void 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}
67void 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}
73void 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}
79void 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}
85void 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}
91void 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}
97void 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}
103void 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}
109void 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}
115void 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}
121void 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}
127void 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}
133void 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