| 1 |
// RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=core,cplusplus,alpha.cplusplus.InvalidatedIterator -analyzer-config aggressive-binary-operation-simplification=true -analyzer-config c++-container-inlining=false %s -verify |
| 2 |
// RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=core,cplusplus,alpha.cplusplus.InvalidatedIterator -analyzer-config aggressive-binary-operation-simplification=true -analyzer-config c++-container-inlining=true -DINLINE=1 %s -verify |
| 3 |
// RUN: %clang_analyze_cc1 -std=c++23 -analyzer-checker=core,cplusplus,alpha.cplusplus.InvalidatedIterator -analyzer-config aggressive-binary-operation-simplification=true -analyzer-config c++-container-inlining=true -DINLINE=1 %s -verify |
| 4 |
|
| 5 |
#include "Inputs/system-header-simulator-cxx.h" |
| 6 |
|
| 7 |
void clang_analyzer_warnIfReached(); |
| 8 |
|
| 9 |
void normal_dereference(std::vector<int> &V) { |
| 10 |
auto i = V.cbegin(); |
| 11 |
*i; // no-warning |
| 12 |
} |
| 13 |
|
| 14 |
void invalidated_dereference(std::vector<int> &V) { |
| 15 |
auto i = V.cbegin(); |
| 16 |
V.erase(i); |
| 17 |
*i; // expected-warning{{Invalidated iterator accessed}} |
| 18 |
} |
| 19 |
|
| 20 |
void normal_prefix_increment(std::vector<int> &V) { |
| 21 |
auto i = V.cbegin(); |
| 22 |
++i; // no-warning |
| 23 |
} |
| 24 |
|
| 25 |
void invalidated_prefix_increment(std::vector<int> &V) { |
| 26 |
auto i = V.cbegin(); |
| 27 |
V.erase(i); |
| 28 |
++i; // expected-warning{{Invalidated iterator accessed}} |
| 29 |
} |
| 30 |
|
| 31 |
void normal_prefix_decrement(std::vector<int> &V) { |
| 32 |
auto i = ++V.cbegin(); |
| 33 |
--i; // no-warning |
| 34 |
} |
| 35 |
|
| 36 |
void invalidated_prefix_decrement(std::vector<int> &V) { |
| 37 |
auto i = ++V.cbegin(); |
| 38 |
V.erase(i); |
| 39 |
--i; // expected-warning{{Invalidated iterator accessed}} |
| 40 |
} |
| 41 |
|
| 42 |
void normal_postfix_increment(std::vector<int> &V) { |
| 43 |
auto i = V.cbegin(); |
| 44 |
i++; // no-warning |
| 45 |
} |
| 46 |
|
| 47 |
void invalidated_postfix_increment(std::vector<int> &V) { |
| 48 |
auto i = V.cbegin(); |
| 49 |
V.erase(i); |
| 50 |
i++; // expected-warning{{Invalidated iterator accessed}} |
| 51 |
} |
| 52 |
|
| 53 |
void normal_postfix_decrement(std::vector<int> &V) { |
| 54 |
auto i = ++V.cbegin(); |
| 55 |
i--; // no-warning |
| 56 |
} |
| 57 |
|
| 58 |
void invalidated_postfix_decrement(std::vector<int> &V) { |
| 59 |
auto i = ++V.cbegin(); |
| 60 |
V.erase(i); |
| 61 |
i--; // expected-warning{{Invalidated iterator accessed}} |
| 62 |
} |
| 63 |
|
| 64 |
void normal_increment_by_2(std::vector<int> &V) { |
| 65 |
auto i = V.cbegin(); |
| 66 |
i += 2; // no-warning |
| 67 |
} |
| 68 |
|
| 69 |
void invalidated_increment_by_2(std::vector<int> &V) { |
| 70 |
auto i = V.cbegin(); |
| 71 |
V.erase(i); |
| 72 |
i += 2; // expected-warning{{Invalidated iterator accessed}} |
| 73 |
} |
| 74 |
|
| 75 |
void normal_increment_by_2_copy(std::vector<int> &V) { |
| 76 |
auto i = V.cbegin(); |
| 77 |
auto j = i + 2; // no-warning |
| 78 |
} |
| 79 |
|
| 80 |
void invalidated_increment_by_2_copy(std::vector<int> &V) { |
| 81 |
auto i = V.cbegin(); |
| 82 |
V.erase(i); |
| 83 |
auto j = i + 2; // expected-warning{{Invalidated iterator accessed}} |
| 84 |
} |
| 85 |
|
| 86 |
void normal_decrement_by_2(std::vector<int> &V) { |
| 87 |
auto i = V.cbegin(); |
| 88 |
i -= 2; // no-warning |
| 89 |
} |
| 90 |
|
| 91 |
void invalidated_decrement_by_2(std::vector<int> &V) { |
| 92 |
auto i = V.cbegin(); |
| 93 |
V.erase(i); |
| 94 |
i -= 2; // expected-warning{{Invalidated iterator accessed}} |
| 95 |
} |
| 96 |
|
| 97 |
void normal_decrement_by_2_copy(std::vector<int> &V) { |
| 98 |
auto i = V.cbegin(); |
| 99 |
auto j = i - 2; // no-warning |
| 100 |
} |
| 101 |
|
| 102 |
void invalidated_decrement_by_2_copy(std::vector<int> &V) { |
| 103 |
auto i = V.cbegin(); |
| 104 |
V.erase(i); |
| 105 |
auto j = i - 2; // expected-warning{{Invalidated iterator accessed}} |
| 106 |
} |
| 107 |
|
| 108 |
void normal_subscript(std::vector<int> &V) { |
| 109 |
auto i = V.cbegin(); |
| 110 |
i[1]; // no-warning |
| 111 |
} |
| 112 |
|
| 113 |
void invalidated_subscript(std::vector<int> &V) { |
| 114 |
auto i = V.cbegin(); |
| 115 |
V.erase(i); |
| 116 |
i[1]; // expected-warning{{Invalidated iterator accessed}} |
| 117 |
} |
| 118 |
|
| 119 |
void assignment(std::vector<int> &V) { |
| 120 |
auto i = V.cbegin(); |
| 121 |
V.erase(i); |
| 122 |
auto j = V.cbegin(); // no-warning |
| 123 |
} |
| 124 |
|
| 125 |
template<typename T> |
| 126 |
struct cont_with_ptr_iterator { |
| 127 |
T *begin() const; |
| 128 |
T *end() const; |
| 129 |
T &operator[](size_t); |
| 130 |
void push_back(const T&); |
| 131 |
T* erase(T*); |
| 132 |
}; |
| 133 |
|
| 134 |
void invalidated_access_via_end_iterator_after_push_back() { |
| 135 |
cont_with_ptr_iterator<int> C; |
| 136 |
C.push_back(1); |
| 137 |
auto i = C.end(); |
| 138 |
C.push_back(2); |
| 139 |
auto j = i[-1]; // expected-warning{{Invalidated iterator accessed}} |
| 140 |
} |
| 141 |
|
| 142 |
void invalidated_dereference_end_ptr_iterator(cont_with_ptr_iterator<int> &C) { |
| 143 |
auto i = C.begin(); |
| 144 |
C.erase(i); |
| 145 |
(void) *i; // expected-warning{{Invalidated iterator accessed}} |
| 146 |
} |
| 147 |
|
| 148 |
void invalidated_prefix_increment_end_ptr_iterator( |
| 149 |
cont_with_ptr_iterator<int> &C) { |
| 150 |
auto i = C.begin(); |
| 151 |
C.erase(i); |
| 152 |
++i; // expected-warning{{Invalidated iterator accessed}} |
| 153 |
} |
| 154 |
|
| 155 |
void invalidated_prefix_decrement_end_ptr_iterator( |
| 156 |
cont_with_ptr_iterator<int> &C) { |
| 157 |
auto i = C.begin() + 1; |
| 158 |
C.erase(i); |
| 159 |
--i; // expected-warning{{Invalidated iterator accessed}} |
| 160 |
} |
| 161 |
|
| 162 |
void invalidated_postfix_increment_end_ptr_iterator( |
| 163 |
cont_with_ptr_iterator<int> &C) { |
| 164 |
auto i = C.begin(); |
| 165 |
C.erase(i); |
| 166 |
i++; // expected-warning{{Invalidated iterator accessed}} |
| 167 |
} |
| 168 |
|
| 169 |
void invalidated_postfix_decrement_end_ptr_iterator( |
| 170 |
cont_with_ptr_iterator<int> &C) { |
| 171 |
auto i = C.begin() + 1; |
| 172 |
C.erase(i); |
| 173 |
i--; // expected-warning{{Invalidated iterator accessed}} |
| 174 |
} |
| 175 |
|
| 176 |
void invalidated_increment_by_2_end_ptr_iterator( |
| 177 |
cont_with_ptr_iterator<int> &C) { |
| 178 |
auto i = C.begin(); |
| 179 |
C.erase(i); |
| 180 |
i += 2; // expected-warning{{Invalidated iterator accessed}} |
| 181 |
} |
| 182 |
|
| 183 |
void invalidated_increment_by_2_copy_end_ptr_iterator( |
| 184 |
cont_with_ptr_iterator<int> &C) { |
| 185 |
auto i = C.begin(); |
| 186 |
C.erase(i); |
| 187 |
auto j = i + 2; // expected-warning{{Invalidated iterator accessed}} |
| 188 |
} |
| 189 |
|
| 190 |
void invalidated_decrement_by_2_end_ptr_iterator( |
| 191 |
cont_with_ptr_iterator<int> &C) { |
| 192 |
auto i = C.begin(); |
| 193 |
C.erase(i); |
| 194 |
i -= 2; // expected-warning{{Invalidated iterator accessed}} |
| 195 |
} |
| 196 |
|
| 197 |
void invalidated_decrement_by_2_copy_end_ptr_iterator( |
| 198 |
cont_with_ptr_iterator<int> &C) { |
| 199 |
auto i = C.begin(); |
| 200 |
C.erase(i); |
| 201 |
auto j = i - 2; // expected-warning{{Invalidated iterator accessed}} |
| 202 |
} |
| 203 |
|
| 204 |
void invalidated_subscript_end_ptr_iterator(cont_with_ptr_iterator<int> &C) { |
| 205 |
auto i = C.begin(); |
| 206 |
C.erase(i); |
| 207 |
(void) i[1]; // expected-warning{{Invalidated iterator accessed}} |
| 208 |
} |
| 209 |
|
| 210 |
#if __cplusplus >= 202302L |
| 211 |
namespace GH116372 { |
| 212 |
class ExplicitThis { |
| 213 |
int f = 0; |
| 214 |
public: |
| 215 |
ExplicitThis(); |
| 216 |
ExplicitThis(ExplicitThis& other); |
| 217 |
|
| 218 |
ExplicitThis& operator=(this ExplicitThis& self, ExplicitThis const& other) { // no crash |
| 219 |
self.f = other.f; |
| 220 |
return self; |
| 221 |
} |
| 222 |
|
| 223 |
~ExplicitThis(); |
| 224 |
}; |
| 225 |
|
| 226 |
void func(ExplicitThis& obj1) { |
| 227 |
obj1 = obj1; |
| 228 |
} |
| 229 |
} |
| 230 |
#endif |