unix-fns.c revision 027a6abdd6cedc0b8203da72eed6d15c796dce9d
1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-check-objc-mem -analyzer-checker=unix.API -analyzer-checker=macosx.API %s -analyzer-store=region -fblocks -verify
2// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-check-objc-mem -analyzer-checker=unix.API -analyzer-checker=macosx.API %s -analyzer-store=basic -fblocks -verify
3
4struct _opaque_pthread_once_t {
5  long __sig;
6  char __opaque[8];
7};
8typedef struct _opaque_pthread_once_t    __darwin_pthread_once_t;
9typedef __darwin_pthread_once_t pthread_once_t;
10int pthread_once(pthread_once_t *, void (*)(void));
11typedef long unsigned int __darwin_size_t;
12typedef __darwin_size_t size_t;
13void *malloc(size_t);
14
15typedef void (^dispatch_block_t)(void);
16typedef long dispatch_once_t;
17void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);
18
19#ifndef O_CREAT
20#define O_CREAT 0x0200
21#define O_RDONLY 0x0000
22#endif
23int open(const char *, int, ...);
24int close(int fildes);
25
26void test_open(const char *path) {
27  int fd;
28  fd = open(path, O_RDONLY); // no-warning
29  if (!fd)
30    close(fd);
31
32  fd = open(path, O_CREAT); // expected-warning{{Call to 'open' requires a third argument when the 'O_CREAT' flag is set}}
33  if (!fd)
34    close(fd);
35}
36
37void test_dispatch_once() {
38  dispatch_once_t pred = 0;
39  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}}
40}
41void test_dispatch_once_neg() {
42  static dispatch_once_t pred = 0;
43  do { if (__builtin_expect(*(&pred), ~0l) != ~0l) dispatch_once((&pred), (^() {})); } while (0); // no-warning
44}
45
46void test_pthread_once_aux();
47
48void test_pthread_once() {
49  pthread_once_t pred = {0x30B1BCBA, {0}};
50  pthread_once(&pred, test_pthread_once_aux); // expected-warning{{Call to 'pthread_once' uses the local variable 'pred' for the "control" value}}
51}
52void test_pthread_once_neg() {
53  static pthread_once_t pred = {0x30B1BCBA, {0}};
54  pthread_once(&pred, test_pthread_once_aux); // no-warning
55}
56
57// PR 2899 - warn of zero-sized allocations to malloc().
58void pr2899() {
59  char* foo = malloc(0); // expected-warning{{Call to 'malloc' has an allocation size of 0 bytes}}
60  for (unsigned i = 0; i < 100; i++) {
61    foo[i] = 0;
62  }
63}
64void pr2899_nowarn(size_t size) {
65  char* foo = malloc(size); // no-warning
66  for (unsigned i = 0; i < 100; i++) {
67    foo[i] = 0;
68  }
69}
70