Zhongxing Xu | 12b49d9 | 2008-11-11 01:25:18 +0000 | [diff] [blame] | 1 | // RUN: clang -std=gnu99 -checker-simple -verify %s && |
Zhongxing Xu | fe81bc2 | 2008-11-10 09:43:12 +0000 | [diff] [blame] | 2 | // RUN: clang -std=gnu99 -checker-simple -analyzer-store-region -verify %s |
Ted Kremenek | 2f54af4 | 2008-04-02 16:54:39 +0000 | [diff] [blame] | 3 | |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 4 | #include<stdint.h> |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 5 | #include <assert.h> |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 6 | |
Ted Kremenek | 2f54af4 | 2008-04-02 16:54:39 +0000 | [diff] [blame] | 7 | void f1(int *p) { |
| 8 | if (p) *p = 1; |
| 9 | else *p = 0; // expected-warning{{ereference}} |
| 10 | } |
Ted Kremenek | b9ab690 | 2008-04-21 23:44:17 +0000 | [diff] [blame] | 11 | |
| 12 | struct foo_struct { |
| 13 | int x; |
| 14 | }; |
| 15 | |
| 16 | int f2(struct foo_struct* p) { |
| 17 | |
| 18 | if (p) |
| 19 | p->x = 1; |
| 20 | |
Ted Kremenek | 3603d73 | 2008-04-21 23:45:26 +0000 | [diff] [blame] | 21 | return p->x++; // expected-warning{{Dereference of null pointer.}} |
Ted Kremenek | b9ab690 | 2008-04-21 23:44:17 +0000 | [diff] [blame] | 22 | } |
Ted Kremenek | 9704eac | 2008-04-22 04:56:55 +0000 | [diff] [blame] | 23 | |
| 24 | int f3(char* x) { |
| 25 | |
| 26 | int i = 2; |
| 27 | |
| 28 | if (x) |
| 29 | return x[i - 1]; |
| 30 | |
| 31 | return x[i+1]; // expected-warning{{Dereference of null pointer.}} |
| 32 | } |
| 33 | |
Ted Kremenek | e2013f5 | 2008-04-29 23:25:09 +0000 | [diff] [blame] | 34 | int f3_b(char* x) { |
| 35 | |
| 36 | int i = 2; |
| 37 | |
| 38 | if (x) |
| 39 | return x[i - 1]; |
| 40 | |
| 41 | return x[i+1]++; // expected-warning{{Dereference of null pointer.}} |
| 42 | } |
| 43 | |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 44 | int f4(int *p) { |
| 45 | |
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 46 | uintptr_t x = (uintptr_t) p; |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 47 | |
| 48 | if (x) |
| 49 | return 1; |
| 50 | |
| 51 | int *q = (int*) x; |
| 52 | return *q; // expected-warning{{Dereference of null pointer.}} |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | int f5() { |
| 56 | |
| 57 | char *s = "hello world"; |
| 58 | return s[0]; // no-warning |
| 59 | } |
| 60 | |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 61 | int bar(int* p, int q) __attribute__((nonnull)); |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 62 | |
| 63 | int f6(int *p) { |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 64 | return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 65 | : bar(p, 0); // no-warning |
| 66 | } |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 68 | int bar2(int* p, int q) __attribute__((nonnull(1))); |
| 69 | |
| 70 | int f6b(int *p) { |
| 71 | return !p ? bar2(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 72 | : bar2(p, 0); // no-warning |
| 73 | } |
| 74 | |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 75 | int bar3(int*p, int q, int *r) __attribute__((nonnull(1,3))); |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 76 | |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 77 | int f6c(int *p, int *q) { |
Ted Kremenek | a317e90 | 2008-12-04 19:44:23 +0000 | [diff] [blame^] | 78 | return !p ? bar3(q, 2, p) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
| 79 | : bar3(p, 2, q); // no-warning |
Ted Kremenek | 1e10011 | 2008-12-04 19:39:12 +0000 | [diff] [blame] | 80 | } |
Ted Kremenek | a96ac06 | 2008-12-04 18:35:53 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 82 | int* qux(); |
| 83 | |
| 84 | int f7(int x) { |
| 85 | |
| 86 | int* p = 0; |
| 87 | |
| 88 | if (0 == x) |
| 89 | p = qux(); |
| 90 | |
| 91 | if (0 == x) |
| 92 | *p = 1; // no-warning |
| 93 | |
| 94 | return x; |
| 95 | } |
| 96 | |
Ted Kremenek | dd463b8 | 2008-08-16 00:45:40 +0000 | [diff] [blame] | 97 | int f8(int *p, int *q) { |
| 98 | if (!p) |
| 99 | if (p) |
| 100 | *p = 1; // no-warning |
| 101 | |
| 102 | if (q) |
| 103 | if (!q) |
| 104 | *q = 1; // no-warning |
| 105 | } |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 106 | |
| 107 | int* qux(); |
| 108 | |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 109 | int f9(unsigned len) { |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 110 | assert (len != 0); |
| 111 | int *p = 0; |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 112 | unsigned i; |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 114 | for (i = 0; i < len; ++i) |
Ted Kremenek | e2b0083 | 2008-09-16 23:25:28 +0000 | [diff] [blame] | 115 | p = qux(i); |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 116 | |
| 117 | return *p++; // no-warning |
| 118 | } |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 119 | |
Ted Kremenek | 0a41e5a | 2008-09-19 18:00:36 +0000 | [diff] [blame] | 120 | int f9b(unsigned len) { |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 121 | assert (len > 0); // note use of '>' |
| 122 | int *p = 0; |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 123 | unsigned i; |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 124 | |
Ted Kremenek | cafd908 | 2008-09-24 06:40:03 +0000 | [diff] [blame] | 125 | for (i = 0; i < len; ++i) |
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame] | 126 | p = qux(i); |
| 127 | |
| 128 | return *p++; // no-warning |
| 129 | } |
| 130 | |
Ted Kremenek | 973e72a | 2008-11-15 04:44:13 +0000 | [diff] [blame] | 131 | int* f10(int* p, signed char x, int y) { |
| 132 | // This line tests symbolication with compound assignments where the |
| 133 | // LHS and RHS have different bitwidths. The new symbolic value |
| 134 | // for 'x' should have a bitwidth of 8. |
| 135 | x &= y; |
| 136 | |
| 137 | // This tests that our symbolication worked, and that we correctly test |
| 138 | // x against 0 (with the same bitwidth). |
| 139 | if (!x) { |
| 140 | if (!p) return; |
| 141 | *p = 10; |
| 142 | } |
| 143 | else p = 0; |
| 144 | |
| 145 | if (!x) |
| 146 | *p = 5; // no-warning |
| 147 | |
| 148 | return p; |
| 149 | } |
| 150 | |
Ted Kremenek | 73abd13 | 2008-12-03 18:56:12 +0000 | [diff] [blame] | 151 | // Test case from <rdar://problem/6407949> |
| 152 | void f11(unsigned i) { |
| 153 | int *x = 0; |
| 154 | if (i >= 0) { |
| 155 | // always true |
| 156 | } else { |
| 157 | *x = 42; // no-warning |
| 158 | } |
| 159 | } |
| 160 | |
Ted Kremenek | d7ff487 | 2008-12-03 19:06:30 +0000 | [diff] [blame] | 161 | void f11b(unsigned i) { |
| 162 | int *x = 0; |
| 163 | if (i <= ~(unsigned)0) { |
| 164 | // always true |
| 165 | } else { |
| 166 | *x = 42; // no-warning |
| 167 | } |
| 168 | } |
| 169 | |