Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -analyze -std=gnu99 -checker-cfref -verify %s -analyzer-constraints=basic -analyzer-store=basic && |
Ted Kremenek | c037eac | 2009-07-10 00:41:58 +0000 | [diff] [blame] | 2 | // RUN: clang-cc -analyze -std=gnu99 -checker-cfref -verify %s -analyzer-constraints=basic -analyzer-store=basic-old-cast && |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 3 | // RUN: clang-cc -analyze -std=gnu99 -checker-cfref -verify %s -analyzer-constraints=range -analyzer-store=basic && |
Ted Kremenek | c037eac | 2009-07-10 00:41:58 +0000 | [diff] [blame] | 4 | // RUN: clang-cc -analyze -std=gnu99 -checker-cfref -verify %s -analyzer-constraints=range -analyzer-store=basic-old-cast && |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 5 | // RUN: clang-cc -analyze -std=gnu99 -checker-cfref -analyzer-store=region -analyzer-constraints=range -analyzer-purge-dead=false -verify %s && |
| 6 | // RUN: clang-cc -analyze -std=gnu99 -checker-cfref -analyzer-store=region -analyzer-constraints=range -verify %s |
Ted Kremenek | 2f54af4 | 2008-04-02 16:54:39 +0000 | [diff] [blame] | 7 | |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 8 | #include<stdint.h> |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 9 | #include <assert.h> |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 10 | |
Ted Kremenek | 2f54af4 | 2008-04-02 16:54:39 +0000 | [diff] [blame] | 11 | void f1(int *p) { |
| 12 | if (p) *p = 1; |
| 13 | else *p = 0; // expected-warning{{ereference}} |
| 14 | } |
Ted Kremenek | b9ab690 | 2008-04-21 23:44:17 +0000 | [diff] [blame] | 15 | |
| 16 | struct foo_struct { |
| 17 | int x; |
| 18 | }; |
| 19 | |
| 20 | int f2(struct foo_struct* p) { |
| 21 | |
| 22 | if (p) |
| 23 | p->x = 1; |
| 24 | |
Ted Kremenek | 3603d73 | 2008-04-21 23:45:26 +0000 | [diff] [blame] | 25 | return p->x++; // expected-warning{{Dereference of null pointer.}} |
Ted Kremenek | b9ab690 | 2008-04-21 23:44:17 +0000 | [diff] [blame] | 26 | } |
Ted Kremenek | 9704eac | 2008-04-22 04:56:55 +0000 | [diff] [blame] | 27 | |
| 28 | int f3(char* x) { |
| 29 | |
| 30 | int i = 2; |
| 31 | |
| 32 | if (x) |
| 33 | return x[i - 1]; |
| 34 | |
| 35 | return x[i+1]; // expected-warning{{Dereference of null pointer.}} |
| 36 | } |
| 37 | |
Ted Kremenek | e2013f5 | 2008-04-29 23:25:09 +0000 | [diff] [blame] | 38 | int f3_b(char* x) { |
| 39 | |
| 40 | int i = 2; |
| 41 | |
| 42 | if (x) |
| 43 | return x[i - 1]; |
| 44 | |
| 45 | return x[i+1]++; // expected-warning{{Dereference of null pointer.}} |
| 46 | } |
| 47 | |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 48 | int f4(int *p) { |
| 49 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 50 | uintptr_t x = (uintptr_t) p; |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 51 | |
| 52 | if (x) |
| 53 | return 1; |
| 54 | |
| 55 | int *q = (int*) x; |
| 56 | return *q; // expected-warning{{Dereference of null pointer.}} |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Ted Kremenek | e1c2a67 | 2009-01-13 01:04:21 +0000 | [diff] [blame] | 59 | int f4_b() { |
| 60 | short array[2]; |
| 61 | uintptr_t x = array; // expected-warning{{incompatible pointer to integer conversion initializing}} |
| 62 | short *p = x; // expected-warning{{incompatible integer to pointer conversion initializing}} |
| 63 | |
| 64 | // The following branch should be infeasible. |
| 65 | if (!(p = &array[0])) { |
| 66 | p = 0; |
| 67 | *p = 1; // no-warning |
| 68 | } |
| 69 | |
| 70 | if (p) { |
| 71 | *p = 5; // no-warning |
| 72 | p = 0; |
| 73 | } |
Steve Naroff | 2c0ccd0 | 2009-04-30 16:01:26 +0000 | [diff] [blame] | 74 | else return; // expected-warning {{non-void function 'f4_b' should return a value}} |
Ted Kremenek | e1c2a67 | 2009-01-13 01:04:21 +0000 | [diff] [blame] | 75 | |
| 76 | *p += 10; // expected-warning{{Dereference of null pointer}} |
Mike Stump | 339d52a | 2009-07-21 18:51:31 +0000 | [diff] [blame] | 77 | return 0; |
Ted Kremenek | e1c2a67 | 2009-01-13 01:04:21 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 81 | int f5() { |
| 82 | |
| 83 | char *s = "hello world"; |
| 84 | return s[0]; // no-warning |
| 85 | } |
| 86 | |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 87 | int bar(int* p, int q) __attribute__((nonnull)); |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 88 | |
| 89 | int f6(int *p) { |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 90 | return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 91 | : bar(p, 0); // no-warning |
| 92 | } |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 94 | int bar2(int* p, int q) __attribute__((nonnull(1))); |
| 95 | |
| 96 | int f6b(int *p) { |
| 97 | return !p ? bar2(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 98 | : bar2(p, 0); // no-warning |
| 99 | } |
| 100 | |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 101 | int bar3(int*p, int q, int *r) __attribute__((nonnull(1,3))); |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 103 | int f6c(int *p, int *q) { |
Ted Kremenek | a317e90 | 2008-12-04 19:44:23 +0000 | [diff] [blame] | 104 | return !p ? bar3(q, 2, p) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 105 | : bar3(p, 2, q); // no-warning |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 106 | } |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 107 | |
Mike Stump | f0549e2 | 2009-07-22 22:55:09 +0000 | [diff] [blame^] | 108 | void f6d(int *p) { |
Ted Kremenek | c26a8b0 | 2009-07-22 21:46:56 +0000 | [diff] [blame] | 109 | bar(p, 0); |
| 110 | // At this point, 'p' cannot be null. |
| 111 | if (!p) { |
| 112 | int *q = 0; |
| 113 | *q = 0xDEADBEEF; // no-warning |
| 114 | } |
| 115 | } |
| 116 | |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 117 | int* qux(); |
| 118 | |
| 119 | int f7(int x) { |
| 120 | |
| 121 | int* p = 0; |
| 122 | |
| 123 | if (0 == x) |
| 124 | p = qux(); |
| 125 | |
| 126 | if (0 == x) |
| 127 | *p = 1; // no-warning |
| 128 | |
| 129 | return x; |
| 130 | } |
| 131 | |
Ted Kremenek | 935022a | 2009-05-02 00:41:02 +0000 | [diff] [blame] | 132 | int* f7b(int *x) { |
| 133 | |
| 134 | int* p = 0; |
| 135 | |
| 136 | if (((void*)0) == x) |
| 137 | p = qux(); |
| 138 | |
| 139 | if (((void*)0) == x) |
| 140 | *p = 1; // no-warning |
| 141 | |
| 142 | return x; |
| 143 | } |
| 144 | |
Ted Kremenek | 1308f57 | 2009-05-04 17:27:32 +0000 | [diff] [blame] | 145 | int* f7c(int *x) { |
| 146 | |
| 147 | int* p = 0; |
| 148 | |
| 149 | if (((void*)0) == x) |
| 150 | p = qux(); |
| 151 | |
| 152 | if (((void*)0) != x) |
| 153 | return x; |
Ted Kremenek | 65d80fd | 2009-05-04 17:53:11 +0000 | [diff] [blame] | 154 | |
| 155 | // If we reach here then 'p' is not null. |
| 156 | *p = 1; // no-warning |
Ted Kremenek | 1308f57 | 2009-05-04 17:27:32 +0000 | [diff] [blame] | 157 | return x; |
| 158 | } |
| 159 | |
| 160 | int* f7c2(int *x) { |
| 161 | |
| 162 | int* p = 0; |
| 163 | |
| 164 | if (((void*)0) == x) |
| 165 | p = qux(); |
| 166 | |
| 167 | if (((void*)0) == x) |
| 168 | return x; |
| 169 | |
| 170 | *p = 1; // expected-warning{{null}} |
| 171 | return x; |
| 172 | } |
| 173 | |
Ted Kremenek | 935022a | 2009-05-02 00:41:02 +0000 | [diff] [blame] | 174 | |
Mike Stump | 339d52a | 2009-07-21 18:51:31 +0000 | [diff] [blame] | 175 | void f8(int *p, int *q) { |
Ted Kremenek | dd463b8 | 2008-08-16 00:45:40 +0000 | [diff] [blame] | 176 | if (!p) |
| 177 | if (p) |
| 178 | *p = 1; // no-warning |
| 179 | |
| 180 | if (q) |
| 181 | if (!q) |
| 182 | *q = 1; // no-warning |
| 183 | } |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 184 | |
| 185 | int* qux(); |
| 186 | |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 187 | int f9(unsigned len) { |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 188 | assert (len != 0); |
| 189 | int *p = 0; |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 190 | unsigned i; |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 191 | |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 192 | for (i = 0; i < len; ++i) |
Ted Kremenek | e2b0083 | 2008-09-16 23:25:28 +0000 | [diff] [blame] | 193 | p = qux(i); |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 194 | |
| 195 | return *p++; // no-warning |
| 196 | } |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 197 | |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 198 | int f9b(unsigned len) { |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 199 | assert (len > 0); // note use of '>' |
| 200 | int *p = 0; |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 201 | unsigned i; |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 203 | for (i = 0; i < len; ++i) |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 204 | p = qux(i); |
| 205 | |
| 206 | return *p++; // no-warning |
| 207 | } |
| 208 | |
Ted Kremenek | 973e72a | 2008-11-15 04:44:13 +0000 | [diff] [blame] | 209 | int* f10(int* p, signed char x, int y) { |
| 210 | // This line tests symbolication with compound assignments where the |
| 211 | // LHS and RHS have different bitwidths. The new symbolic value |
| 212 | // for 'x' should have a bitwidth of 8. |
| 213 | x &= y; |
| 214 | |
| 215 | // This tests that our symbolication worked, and that we correctly test |
| 216 | // x against 0 (with the same bitwidth). |
| 217 | if (!x) { |
Steve Naroff | 2c0ccd0 | 2009-04-30 16:01:26 +0000 | [diff] [blame] | 218 | if (!p) return; // expected-warning {{non-void function 'f10' should return a value}} |
Ted Kremenek | 973e72a | 2008-11-15 04:44:13 +0000 | [diff] [blame] | 219 | *p = 10; |
| 220 | } |
| 221 | else p = 0; |
| 222 | |
| 223 | if (!x) |
| 224 | *p = 5; // no-warning |
| 225 | |
| 226 | return p; |
| 227 | } |
| 228 | |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 229 | // Test case from <rdar://problem/6407949> |
| 230 | void f11(unsigned i) { |
| 231 | int *x = 0; |
| 232 | if (i >= 0) { |
| 233 | // always true |
| 234 | } else { |
| 235 | *x = 42; // no-warning |
| 236 | } |
| 237 | } |
| 238 | |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 239 | void f11b(unsigned i) { |
| 240 | int *x = 0; |
| 241 | if (i <= ~(unsigned)0) { |
| 242 | // always true |
| 243 | } else { |
| 244 | *x = 42; // no-warning |
| 245 | } |
| 246 | } |
| 247 | |
Ted Kremenek | 72afb37 | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 248 | // Test case for switch statements with weird case arms. |
| 249 | typedef int BOOL, *PBOOL, *LPBOOL; |
| 250 | typedef long LONG_PTR, *PLONG_PTR; |
| 251 | typedef unsigned long ULONG_PTR, *PULONG_PTR; |
| 252 | typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR; |
| 253 | typedef LONG_PTR LRESULT; |
| 254 | typedef struct _F12ITEM *HF12ITEM; |
| 255 | |
| 256 | void f12(HF12ITEM i, char *q) { |
| 257 | char *p = 0; |
| 258 | switch ((DWORD_PTR) i) { |
| 259 | case 0 ... 10: |
| 260 | p = q; |
| 261 | break; |
| 262 | case (DWORD_PTR) ((HF12ITEM) - 65535): |
| 263 | return; |
| 264 | default: |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | *p = 1; // no-warning |
| 269 | } |
| 270 | |
Ted Kremenek | efcfcc0 | 2009-03-05 02:42:32 +0000 | [diff] [blame] | 271 | // Test handling of translating between integer "pointers" and back. |
| 272 | void f13() { |
| 273 | int *x = 0; |
| 274 | if (((((int) x) << 2) + 1) >> 1) *x = 1; // no-warning |
| 275 | } |
| 276 | |
| 277 | |