Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 1 | // 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 | |
| 4 | struct _opaque_pthread_once_t { |
| 5 | long __sig; |
| 6 | char __opaque[8]; |
| 7 | }; |
| 8 | typedef struct _opaque_pthread_once_t __darwin_pthread_once_t; |
| 9 | typedef __darwin_pthread_once_t pthread_once_t; |
| 10 | int pthread_once(pthread_once_t *, void (*)(void)); |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 11 | typedef long unsigned int __darwin_size_t; |
| 12 | typedef __darwin_size_t size_t; |
| 13 | void *malloc(size_t); |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 14 | |
| 15 | typedef void (^dispatch_block_t)(void); |
| 16 | typedef long dispatch_once_t; |
| 17 | void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 18 | |
Ted Kremenek | fc89323 | 2010-02-25 01:16:07 +0000 | [diff] [blame] | 19 | #ifndef O_CREAT |
| 20 | #define O_CREAT 0x0200 |
| 21 | #define O_RDONLY 0x0000 |
| 22 | #endif |
| 23 | int open(const char *, int, ...); |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 24 | int close(int fildes); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 25 | |
| 26 | void 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 Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 36 | |
| 37 | void 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 | } |
| 41 | void 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 | |
| 46 | void test_pthread_once_aux(); |
| 47 | |
| 48 | void 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 | } |
| 52 | void test_pthread_once_neg() { |
| 53 | static pthread_once_t pred = {0x30B1BCBA, {0}}; |
| 54 | pthread_once(&pred, test_pthread_once_aux); // no-warning |
| 55 | } |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 56 | |
| 57 | // PR 2899 - warn of zero-sized allocations to malloc(). |
| 58 | void 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 | } |
| 64 | void 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 | } |