Ted Kremenek | 5188507 | 2011-03-24 00:28:47 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=unix.API,osx.API %s -analyzer-store=region -fblocks -verify |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 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)); |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 10 | typedef long unsigned int __darwin_size_t; |
| 11 | typedef __darwin_size_t size_t; |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 12 | void *calloc(size_t, size_t); |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 13 | void *malloc(size_t); |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 14 | void *realloc(void *, size_t); |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 15 | void *alloca(size_t); |
| 16 | void *valloc(size_t); |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 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); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 21 | |
Ted Kremenek | fc89323 | 2010-02-25 01:16:07 +0000 | [diff] [blame] | 22 | #ifndef O_CREAT |
| 23 | #define O_CREAT 0x0200 |
| 24 | #define O_RDONLY 0x0000 |
| 25 | #endif |
| 26 | int open(const char *, int, ...); |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 27 | int close(int fildes); |
Ted Kremenek | 381d1bf | 2010-02-25 00:20:35 +0000 | [diff] [blame] | 28 | |
| 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 | } |
Ted Kremenek | 99d9838 | 2010-04-08 19:53:31 +0000 | [diff] [blame] | 39 | |
| 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 | } |
| 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 | |
| 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 | } |
| 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 | } |
Ted Kremenek | b12fbc2 | 2010-11-16 18:47:04 +0000 | [diff] [blame] | 59 | |
| 60 | // PR 2899 - warn of zero-sized allocations to malloc(). |
| 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 | } |
| 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 | } |
Ted Kremenek | c1275da | 2012-01-03 23:43:13 +0000 | [diff] [blame] | 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 | } |
| 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 | } |
| 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 | } |
| 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 | } |
| 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 | } |
Ted Kremenek | 3e97758 | 2012-01-11 08:13:21 +0000 | [diff] [blame] | 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 | } |
| 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 | } |
| 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 | } |
| 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 | } |
| 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 | } |
| 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 | } |