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