blob: e348beb322dd3df6bce3a860565b5518717b57d6 [file] [log] [blame]
Ted Kremenek99d98382010-04-08 19:53:31 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-check-objc-mem %s -analyzer-store=region -fblocks -verify
2// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-check-objc-mem %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));
Ted Kremenekb12fbc22010-11-16 18:47:04 +000011typedef long unsigned int __darwin_size_t;
12typedef __darwin_size_t size_t;
13void *malloc(size_t);
Ted Kremenek99d98382010-04-08 19:53:31 +000014
15typedef void (^dispatch_block_t)(void);
16typedef long dispatch_once_t;
17void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);
Ted Kremenek381d1bf2010-02-25 00:20:35 +000018
Ted Kremenekfc893232010-02-25 01:16:07 +000019#ifndef O_CREAT
20#define O_CREAT 0x0200
21#define O_RDONLY 0x0000
22#endif
23int open(const char *, int, ...);
Ted Kremenek99d98382010-04-08 19:53:31 +000024int close(int fildes);
Ted Kremenek381d1bf2010-02-25 00:20:35 +000025
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}
Ted Kremenek99d98382010-04-08 19:53:31 +000036
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}
Ted Kremenekb12fbc22010-11-16 18:47:04 +000056
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}