Daniel Dunbar | 9c32110 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 1 | // RUN: clang -analyze -std=gnu99 -checker-simple -verify %s && |
Ted Kremenek | ad1863e | 2009-01-22 22:46:40 +0000 | [diff] [blame^] | 2 | // RUN: clang -analyze -std=gnu99 -checker-simple -analyzer-store-region -analyzer-purge-dead=false -verify %s && |
| 3 | // RUN: clang -analyze -std=gnu99 -checker-cfref -analyzer-store-region -verify %s |
Ted Kremenek | 97bfee8 | 2008-04-02 16:54:39 +0000 | [diff] [blame] | 4 | |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 5 | #include<stdint.h> |
Ted Kremenek | 7603ed8 | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 6 | #include <assert.h> |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 7 | |
Ted Kremenek | 97bfee8 | 2008-04-02 16:54:39 +0000 | [diff] [blame] | 8 | void f1(int *p) { |
| 9 | if (p) *p = 1; |
| 10 | else *p = 0; // expected-warning{{ereference}} |
| 11 | } |
Ted Kremenek | 6421648 | 2008-04-21 23:44:17 +0000 | [diff] [blame] | 12 | |
| 13 | struct foo_struct { |
| 14 | int x; |
| 15 | }; |
| 16 | |
| 17 | int f2(struct foo_struct* p) { |
| 18 | |
| 19 | if (p) |
| 20 | p->x = 1; |
| 21 | |
Ted Kremenek | 1792bc4 | 2008-04-21 23:45:26 +0000 | [diff] [blame] | 22 | return p->x++; // expected-warning{{Dereference of null pointer.}} |
Ted Kremenek | 6421648 | 2008-04-21 23:44:17 +0000 | [diff] [blame] | 23 | } |
Ted Kremenek | 58fe0eb | 2008-04-22 04:56:55 +0000 | [diff] [blame] | 24 | |
| 25 | int f3(char* x) { |
| 26 | |
| 27 | int i = 2; |
| 28 | |
| 29 | if (x) |
| 30 | return x[i - 1]; |
| 31 | |
| 32 | return x[i+1]; // expected-warning{{Dereference of null pointer.}} |
| 33 | } |
| 34 | |
Ted Kremenek | 62d8edb | 2008-04-29 23:25:09 +0000 | [diff] [blame] | 35 | int f3_b(char* x) { |
| 36 | |
| 37 | int i = 2; |
| 38 | |
| 39 | if (x) |
| 40 | return x[i - 1]; |
| 41 | |
| 42 | return x[i+1]++; // expected-warning{{Dereference of null pointer.}} |
| 43 | } |
| 44 | |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 45 | int f4(int *p) { |
| 46 | |
Daniel Dunbar | 103baef | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 47 | uintptr_t x = (uintptr_t) p; |
Ted Kremenek | fe1a0b1 | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 48 | |
| 49 | if (x) |
| 50 | return 1; |
| 51 | |
| 52 | int *q = (int*) x; |
| 53 | return *q; // expected-warning{{Dereference of null pointer.}} |
Ted Kremenek | be62129 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Ted Kremenek | 16354a4 | 2009-01-13 01:04:21 +0000 | [diff] [blame] | 56 | int f4_b() { |
| 57 | short array[2]; |
| 58 | uintptr_t x = array; // expected-warning{{incompatible pointer to integer conversion initializing}} |
| 59 | short *p = x; // expected-warning{{incompatible integer to pointer conversion initializing}} |
| 60 | |
| 61 | // The following branch should be infeasible. |
| 62 | if (!(p = &array[0])) { |
| 63 | p = 0; |
| 64 | *p = 1; // no-warning |
| 65 | } |
| 66 | |
| 67 | if (p) { |
| 68 | *p = 5; // no-warning |
| 69 | p = 0; |
| 70 | } |
| 71 | else return; |
| 72 | |
| 73 | *p += 10; // expected-warning{{Dereference of null pointer}} |
| 74 | } |
| 75 | |
| 76 | |
Ted Kremenek | be62129 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 77 | int f5() { |
| 78 | |
| 79 | char *s = "hello world"; |
| 80 | return s[0]; // no-warning |
| 81 | } |
| 82 | |
Ted Kremenek | 7af441b | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 83 | int bar(int* p, int q) __attribute__((nonnull)); |
Ted Kremenek | bbafa5b | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 84 | |
| 85 | int f6(int *p) { |
Ted Kremenek | 7af441b | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 86 | return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 87 | : bar(p, 0); // no-warning |
| 88 | } |
Ted Kremenek | bbafa5b | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | 7cf9047 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 90 | int bar2(int* p, int q) __attribute__((nonnull(1))); |
| 91 | |
| 92 | int f6b(int *p) { |
| 93 | return !p ? bar2(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 94 | : bar2(p, 0); // no-warning |
| 95 | } |
| 96 | |
Ted Kremenek | a7eab66 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 97 | int bar3(int*p, int q, int *r) __attribute__((nonnull(1,3))); |
Ted Kremenek | 7cf9047 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | a7eab66 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 99 | int f6c(int *p, int *q) { |
Ted Kremenek | fed204c | 2008-12-04 19:44:23 +0000 | [diff] [blame] | 100 | return !p ? bar3(q, 2, p) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 101 | : bar3(p, 2, q); // no-warning |
Ted Kremenek | a7eab66 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 102 | } |
Ted Kremenek | 7cf9047 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 103 | |
Ted Kremenek | 0303bab | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 104 | int* qux(); |
| 105 | |
| 106 | int f7(int x) { |
| 107 | |
| 108 | int* p = 0; |
| 109 | |
| 110 | if (0 == x) |
| 111 | p = qux(); |
| 112 | |
| 113 | if (0 == x) |
| 114 | *p = 1; // no-warning |
| 115 | |
| 116 | return x; |
| 117 | } |
| 118 | |
Ted Kremenek | f3d8263 | 2008-08-16 00:45:40 +0000 | [diff] [blame] | 119 | int f8(int *p, int *q) { |
| 120 | if (!p) |
| 121 | if (p) |
| 122 | *p = 1; // no-warning |
| 123 | |
| 124 | if (q) |
| 125 | if (!q) |
| 126 | *q = 1; // no-warning |
| 127 | } |
Ted Kremenek | 7603ed8 | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 128 | |
| 129 | int* qux(); |
| 130 | |
Ted Kremenek | 7abe689 | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 131 | int f9(unsigned len) { |
Ted Kremenek | 7603ed8 | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 132 | assert (len != 0); |
| 133 | int *p = 0; |
Ted Kremenek | 64589d0 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 134 | unsigned i; |
Ted Kremenek | 7603ed8 | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | 64589d0 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 136 | for (i = 0; i < len; ++i) |
Ted Kremenek | 34c02bf | 2008-09-16 23:25:28 +0000 | [diff] [blame] | 137 | p = qux(i); |
Ted Kremenek | 7603ed8 | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 138 | |
| 139 | return *p++; // no-warning |
| 140 | } |
Ted Kremenek | 6dd8849 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 7abe689 | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 142 | int f9b(unsigned len) { |
Ted Kremenek | 6dd8849 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 143 | assert (len > 0); // note use of '>' |
| 144 | int *p = 0; |
Ted Kremenek | 64589d0 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 145 | unsigned i; |
Ted Kremenek | 6dd8849 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | 64589d0 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 147 | for (i = 0; i < len; ++i) |
Ted Kremenek | 6dd8849 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 148 | p = qux(i); |
| 149 | |
| 150 | return *p++; // no-warning |
| 151 | } |
| 152 | |
Ted Kremenek | e54459b | 2008-11-15 04:44:13 +0000 | [diff] [blame] | 153 | int* f10(int* p, signed char x, int y) { |
| 154 | // This line tests symbolication with compound assignments where the |
| 155 | // LHS and RHS have different bitwidths. The new symbolic value |
| 156 | // for 'x' should have a bitwidth of 8. |
| 157 | x &= y; |
| 158 | |
| 159 | // This tests that our symbolication worked, and that we correctly test |
| 160 | // x against 0 (with the same bitwidth). |
| 161 | if (!x) { |
| 162 | if (!p) return; |
| 163 | *p = 10; |
| 164 | } |
| 165 | else p = 0; |
| 166 | |
| 167 | if (!x) |
| 168 | *p = 5; // no-warning |
| 169 | |
| 170 | return p; |
| 171 | } |
| 172 | |
Ted Kremenek | a4c51ec | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 173 | // Test case from <rdar://problem/6407949> |
| 174 | void f11(unsigned i) { |
| 175 | int *x = 0; |
| 176 | if (i >= 0) { |
| 177 | // always true |
| 178 | } else { |
| 179 | *x = 42; // no-warning |
| 180 | } |
| 181 | } |
| 182 | |
Ted Kremenek | d91bb6c | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 183 | void f11b(unsigned i) { |
| 184 | int *x = 0; |
| 185 | if (i <= ~(unsigned)0) { |
| 186 | // always true |
| 187 | } else { |
| 188 | *x = 42; // no-warning |
| 189 | } |
| 190 | } |
| 191 | |
Ted Kremenek | 7f6c3a2 | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 192 | // Test case for switch statements with weird case arms. |
| 193 | typedef int BOOL, *PBOOL, *LPBOOL; |
| 194 | typedef long LONG_PTR, *PLONG_PTR; |
| 195 | typedef unsigned long ULONG_PTR, *PULONG_PTR; |
| 196 | typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR; |
| 197 | typedef LONG_PTR LRESULT; |
| 198 | typedef struct _F12ITEM *HF12ITEM; |
| 199 | |
| 200 | void f12(HF12ITEM i, char *q) { |
| 201 | char *p = 0; |
| 202 | switch ((DWORD_PTR) i) { |
| 203 | case 0 ... 10: |
| 204 | p = q; |
| 205 | break; |
| 206 | case (DWORD_PTR) ((HF12ITEM) - 65535): |
| 207 | return; |
| 208 | default: |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | *p = 1; // no-warning |
| 213 | } |
| 214 | |