Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 1 | // RUN: clang -analyze -std=gnu99 -checker-simple -verify %s && |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 2 | // RUN: clang -analyze -std=gnu99 -checker-simple -verify %s -analyzer-constraints=range && |
| 3 | // RUN: clang -analyze -std=gnu99 -checker-simple -analyzer-store=region -analyzer-purge-dead=false -verify %s && |
| 4 | // RUN: clang -analyze -std=gnu99 -checker-cfref -analyzer-store=region -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 | } |
| 72 | else return; |
| 73 | |
| 74 | *p += 10; // expected-warning{{Dereference of null pointer}} |
| 75 | } |
| 76 | |
| 77 | |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 78 | int f5() { |
| 79 | |
| 80 | char *s = "hello world"; |
| 81 | return s[0]; // no-warning |
| 82 | } |
| 83 | |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 84 | int bar(int* p, int q) __attribute__((nonnull)); |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 85 | |
| 86 | int f6(int *p) { |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 87 | return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 88 | : bar(p, 0); // no-warning |
| 89 | } |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 90 | |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 91 | int bar2(int* p, int q) __attribute__((nonnull(1))); |
| 92 | |
| 93 | int f6b(int *p) { |
| 94 | return !p ? bar2(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 95 | : bar2(p, 0); // no-warning |
| 96 | } |
| 97 | |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 98 | int bar3(int*p, int q, int *r) __attribute__((nonnull(1,3))); |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 100 | int f6c(int *p, int *q) { |
Ted Kremenek | a317e90 | 2008-12-04 19:44:23 +0000 | [diff] [blame] | 101 | return !p ? bar3(q, 2, p) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 102 | : bar3(p, 2, q); // no-warning |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 103 | } |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 105 | int* qux(); |
| 106 | |
| 107 | int f7(int x) { |
| 108 | |
| 109 | int* p = 0; |
| 110 | |
| 111 | if (0 == x) |
| 112 | p = qux(); |
| 113 | |
| 114 | if (0 == x) |
| 115 | *p = 1; // no-warning |
| 116 | |
| 117 | return x; |
| 118 | } |
| 119 | |
Ted Kremenek | dd463b8 | 2008-08-16 00:45:40 +0000 | [diff] [blame] | 120 | int f8(int *p, int *q) { |
| 121 | if (!p) |
| 122 | if (p) |
| 123 | *p = 1; // no-warning |
| 124 | |
| 125 | if (q) |
| 126 | if (!q) |
| 127 | *q = 1; // no-warning |
| 128 | } |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 129 | |
| 130 | int* qux(); |
| 131 | |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 132 | int f9(unsigned len) { |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 133 | assert (len != 0); |
| 134 | int *p = 0; |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 135 | unsigned i; |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 137 | for (i = 0; i < len; ++i) |
Ted Kremenek | e2b0083 | 2008-09-16 23:25:28 +0000 | [diff] [blame] | 138 | p = qux(i); |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 139 | |
| 140 | return *p++; // no-warning |
| 141 | } |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 142 | |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 143 | int f9b(unsigned len) { |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 144 | assert (len > 0); // note use of '>' |
| 145 | int *p = 0; |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 146 | unsigned i; |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 148 | for (i = 0; i < len; ++i) |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 149 | p = qux(i); |
| 150 | |
| 151 | return *p++; // no-warning |
| 152 | } |
| 153 | |
Ted Kremenek | 973e72a | 2008-11-15 04:44:13 +0000 | [diff] [blame] | 154 | int* f10(int* p, signed char x, int y) { |
| 155 | // This line tests symbolication with compound assignments where the |
| 156 | // LHS and RHS have different bitwidths. The new symbolic value |
| 157 | // for 'x' should have a bitwidth of 8. |
| 158 | x &= y; |
| 159 | |
| 160 | // This tests that our symbolication worked, and that we correctly test |
| 161 | // x against 0 (with the same bitwidth). |
| 162 | if (!x) { |
| 163 | if (!p) return; |
| 164 | *p = 10; |
| 165 | } |
| 166 | else p = 0; |
| 167 | |
| 168 | if (!x) |
| 169 | *p = 5; // no-warning |
| 170 | |
| 171 | return p; |
| 172 | } |
| 173 | |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 174 | // Test case from <rdar://problem/6407949> |
| 175 | void f11(unsigned i) { |
| 176 | int *x = 0; |
| 177 | if (i >= 0) { |
| 178 | // always true |
| 179 | } else { |
| 180 | *x = 42; // no-warning |
| 181 | } |
| 182 | } |
| 183 | |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 184 | void f11b(unsigned i) { |
| 185 | int *x = 0; |
| 186 | if (i <= ~(unsigned)0) { |
| 187 | // always true |
| 188 | } else { |
| 189 | *x = 42; // no-warning |
| 190 | } |
| 191 | } |
| 192 | |
Ted Kremenek | 72afb37 | 2009-01-17 01:54:16 +0000 | [diff] [blame] | 193 | // Test case for switch statements with weird case arms. |
| 194 | typedef int BOOL, *PBOOL, *LPBOOL; |
| 195 | typedef long LONG_PTR, *PLONG_PTR; |
| 196 | typedef unsigned long ULONG_PTR, *PULONG_PTR; |
| 197 | typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR; |
| 198 | typedef LONG_PTR LRESULT; |
| 199 | typedef struct _F12ITEM *HF12ITEM; |
| 200 | |
| 201 | void f12(HF12ITEM i, char *q) { |
| 202 | char *p = 0; |
| 203 | switch ((DWORD_PTR) i) { |
| 204 | case 0 ... 10: |
| 205 | p = q; |
| 206 | break; |
| 207 | case (DWORD_PTR) ((HF12ITEM) - 65535): |
| 208 | return; |
| 209 | default: |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | *p = 1; // no-warning |
| 214 | } |
| 215 | |
Ted Kremenek | efcfcc0 | 2009-03-05 02:42:32 +0000 | [diff] [blame] | 216 | // Test handling of translating between integer "pointers" and back. |
| 217 | void f13() { |
| 218 | int *x = 0; |
| 219 | if (((((int) x) << 2) + 1) >> 1) *x = 1; // no-warning |
| 220 | } |
| 221 | |
| 222 | |