| 1 |
// RUN: %clang_cc1 -fsyntax-only -Wdangling -Wdangling-field -Wreturn-stack-address -verify %s |
| 2 |
struct [[gsl::Owner(int)]] MyIntOwner { |
| 3 |
MyIntOwner(); |
| 4 |
int &operator*(); |
| 5 |
}; |
| 6 |
|
| 7 |
struct [[gsl::Pointer(int)]] MyIntPointer { |
| 8 |
MyIntPointer(int *p = nullptr); |
| 9 |
// Conversion operator and constructor conversion will result in two |
| 10 |
// different ASTs. The former is tested with another owner and |
| 11 |
// pointer type. |
| 12 |
MyIntPointer(const MyIntOwner &); |
| 13 |
int &operator*(); |
| 14 |
MyIntOwner toOwner(); |
| 15 |
}; |
| 16 |
|
| 17 |
struct MySpecialIntPointer : MyIntPointer { |
| 18 |
}; |
| 19 |
|
| 20 |
// We did see examples in the wild when a derived class changes |
| 21 |
// the ownership model. So we have a test for it. |
| 22 |
struct [[gsl::Owner(int)]] MyOwnerIntPointer : MyIntPointer { |
| 23 |
}; |
| 24 |
|
| 25 |
struct [[gsl::Pointer(long)]] MyLongPointerFromConversion { |
| 26 |
MyLongPointerFromConversion(long *p = nullptr); |
| 27 |
long &operator*(); |
| 28 |
}; |
| 29 |
|
| 30 |
struct [[gsl::Owner(long)]] MyLongOwnerWithConversion { |
| 31 |
MyLongOwnerWithConversion(); |
| 32 |
operator MyLongPointerFromConversion(); |
| 33 |
long &operator*(); |
| 34 |
MyIntPointer releaseAsMyPointer(); |
| 35 |
long *releaseAsRawPointer(); |
| 36 |
}; |
| 37 |
|
| 38 |
void danglingHeapObject() { |
| 39 |
new MyLongPointerFromConversion(MyLongOwnerWithConversion{}); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} |
| 40 |
new MyIntPointer(MyIntOwner{}); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} |
| 41 |
} |
| 42 |
|
| 43 |
void intentionalFalseNegative() { |
| 44 |
int i; |
| 45 |
MyIntPointer p{&i}; |
| 46 |
// In this case we do not have enough information in a statement local |
| 47 |
// analysis to detect the problem. |
| 48 |
new MyIntPointer(p); |
| 49 |
new MyIntPointer(MyIntPointer{p}); |
| 50 |
} |
| 51 |
|
| 52 |
MyIntPointer ownershipTransferToMyPointer() { |
| 53 |
MyLongOwnerWithConversion t; |
| 54 |
return t.releaseAsMyPointer(); // ok |
| 55 |
} |
| 56 |
|
| 57 |
long *ownershipTransferToRawPointer() { |
| 58 |
MyLongOwnerWithConversion t; |
| 59 |
return t.releaseAsRawPointer(); // ok |
| 60 |
} |
| 61 |
|
| 62 |
struct Y { |
| 63 |
int a[4]; |
| 64 |
}; |
| 65 |
|
| 66 |
void dangligGslPtrFromTemporary() { |
| 67 |
MyIntPointer p = Y{}.a; // TODO |
| 68 |
(void)p; |
| 69 |
} |
| 70 |
|
| 71 |
struct DanglingGslPtrField { |
| 72 |
MyIntPointer p; // expected-note {{pointer member declared here}} |
| 73 |
MyLongPointerFromConversion p2; // expected-note {{pointer member declared here}} |
| 74 |
DanglingGslPtrField(int i) : p(&i) {} // TODO |
| 75 |
DanglingGslPtrField() : p2(MyLongOwnerWithConversion{}) {} // expected-warning {{initializing pointer member 'p2' to point to a temporary object whose lifetime is shorter than the lifetime of the constructed object}} |
| 76 |
DanglingGslPtrField(double) : p(MyIntOwner{}) {} // expected-warning {{initializing pointer member 'p' to point to a temporary object whose lifetime is shorter than the lifetime of the constructed object}} |
| 77 |
}; |
| 78 |
|
| 79 |
MyIntPointer danglingGslPtrFromLocal() { |
| 80 |
int j; |
| 81 |
return &j; // TODO |
| 82 |
} |
| 83 |
|
| 84 |
MyIntPointer returningLocalPointer() { |
| 85 |
MyIntPointer localPointer; |
| 86 |
return localPointer; // ok |
| 87 |
} |
| 88 |
|
| 89 |
MyIntPointer daglingGslPtrFromLocalOwner() { |
| 90 |
MyIntOwner localOwner; |
| 91 |
return localOwner; // expected-warning {{address of stack memory associated with local variable 'localOwner' returned}} |
| 92 |
} |
| 93 |
|
| 94 |
MyLongPointerFromConversion daglingGslPtrFromLocalOwnerConv() { |
| 95 |
MyLongOwnerWithConversion localOwner; |
| 96 |
return localOwner; // expected-warning {{address of stack memory associated with local variable 'localOwner' returned}} |
| 97 |
} |
| 98 |
|
| 99 |
MyIntPointer danglingGslPtrFromTemporary() { |
| 100 |
return MyIntOwner{}; // expected-warning {{returning address of local temporary object}} |
| 101 |
} |
| 102 |
|
| 103 |
MyIntOwner makeTempOwner(); |
| 104 |
|
| 105 |
MyIntPointer danglingGslPtrFromTemporary2() { |
| 106 |
return makeTempOwner(); // expected-warning {{returning address of local temporary object}} |
| 107 |
} |
| 108 |
|
| 109 |
MyLongPointerFromConversion danglingGslPtrFromTemporaryConv() { |
| 110 |
return MyLongOwnerWithConversion{}; // expected-warning {{returning address of local temporary object}} |
| 111 |
} |
| 112 |
|
| 113 |
int *noFalsePositive(MyIntOwner &o) { |
| 114 |
MyIntPointer p = o; |
| 115 |
return &*p; // ok |
| 116 |
} |
| 117 |
|
| 118 |
MyIntPointer global; |
| 119 |
MyLongPointerFromConversion global2; |
| 120 |
|
| 121 |
void initLocalGslPtrWithTempOwner() { |
| 122 |
MyIntPointer p = MyIntOwner{}; // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} |
| 123 |
MyIntPointer pp = p = MyIntOwner{}; // expected-warning {{object backing the pointer p will be}} |
| 124 |
p = MyIntOwner{}; // expected-warning {{object backing the pointer p }} |
| 125 |
pp = p; // no warning |
| 126 |
global = MyIntOwner{}; // expected-warning {{object backing the pointer global }} |
| 127 |
MyLongPointerFromConversion p2 = MyLongOwnerWithConversion{}; // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} |
| 128 |
p2 = MyLongOwnerWithConversion{}; // expected-warning {{object backing the pointer p2 }} |
| 129 |
global2 = MyLongOwnerWithConversion{}; // expected-warning {{object backing the pointer global2 }} |
| 130 |
} |
| 131 |
|
| 132 |
namespace __gnu_cxx { |
| 133 |
template <typename T> |
| 134 |
struct basic_iterator { |
| 135 |
basic_iterator operator++(); |
| 136 |
T& operator*() const; |
| 137 |
T* operator->() const; |
| 138 |
}; |
| 139 |
|
| 140 |
template<typename T> |
| 141 |
bool operator!=(basic_iterator<T>, basic_iterator<T>); |
| 142 |
} |
| 143 |
|
| 144 |
namespace std { |
| 145 |
template<typename T> struct remove_reference { typedef T type; }; |
| 146 |
template<typename T> struct remove_reference<T &> { typedef T type; }; |
| 147 |
template<typename T> struct remove_reference<T &&> { typedef T type; }; |
| 148 |
|
| 149 |
template<typename T> |
| 150 |
typename remove_reference<T>::type &&move(T &&t) noexcept; |
| 151 |
|
| 152 |
template <typename C> |
| 153 |
auto data(const C &c) -> decltype(c.data()); |
| 154 |
|
| 155 |
template <typename C> |
| 156 |
auto begin(C &c) -> decltype(c.begin()); |
| 157 |
|
| 158 |
template<typename T, int N> |
| 159 |
T *begin(T (&array)[N]); |
| 160 |
|
| 161 |
template <typename T> |
| 162 |
struct vector { |
| 163 |
typedef __gnu_cxx::basic_iterator<T> iterator; |
| 164 |
iterator begin(); |
| 165 |
iterator end(); |
| 166 |
const T *data() const; |
| 167 |
T &at(int n); |
| 168 |
}; |
| 169 |
|
| 170 |
template<typename T> |
| 171 |
struct basic_string_view { |
| 172 |
basic_string_view(const T *); |
| 173 |
const T *begin() const; |
| 174 |
}; |
| 175 |
using string_view = basic_string_view<char>; |
| 176 |
|
| 177 |
template<class _Mystr> struct iter { |
| 178 |
iter& operator-=(int); |
| 179 |
|
| 180 |
iter operator-(int _Off) const { |
| 181 |
iter _Tmp = *this; |
| 182 |
return _Tmp -= _Off; |
| 183 |
} |
| 184 |
}; |
| 185 |
|
| 186 |
template<typename T> |
| 187 |
struct basic_string { |
| 188 |
basic_string(); |
| 189 |
basic_string(const T *); |
| 190 |
const T *c_str() const; |
| 191 |
operator basic_string_view<T> () const; |
| 192 |
using const_iterator = iter<T>; |
| 193 |
}; |
| 194 |
using string = basic_string<char>; |
| 195 |
|
| 196 |
template<typename T> |
| 197 |
struct unique_ptr { |
| 198 |
T &operator*(); |
| 199 |
T *get() const; |
| 200 |
}; |
| 201 |
|
| 202 |
template<typename T> |
| 203 |
struct optional { |
| 204 |
optional(); |
| 205 |
optional(const T&); |
| 206 |
T &operator*() &; |
| 207 |
T &&operator*() &&; |
| 208 |
T &value() &; |
| 209 |
T &&value() &&; |
| 210 |
}; |
| 211 |
|
| 212 |
template<typename T> |
| 213 |
struct stack { |
| 214 |
T &top(); |
| 215 |
}; |
| 216 |
|
| 217 |
struct any {}; |
| 218 |
|
| 219 |
template<typename T> |
| 220 |
T any_cast(const any& operand); |
| 221 |
|
| 222 |
template<typename T> |
| 223 |
struct reference_wrapper { |
| 224 |
template<typename U> |
| 225 |
reference_wrapper(U &&); |
| 226 |
}; |
| 227 |
|
| 228 |
template<typename T> |
| 229 |
reference_wrapper<T> ref(T& t) noexcept; |
| 230 |
} |
| 231 |
|
| 232 |
struct Unannotated { |
| 233 |
typedef std::vector<int>::iterator iterator; |
| 234 |
iterator begin(); |
| 235 |
operator iterator() const; |
| 236 |
}; |
| 237 |
|
| 238 |
void modelIterators() { |
| 239 |
std::vector<int>::iterator it = std::vector<int>().begin(); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} |
| 240 |
(void)it; |
| 241 |
} |
| 242 |
|
| 243 |
std::vector<int>::iterator modelIteratorReturn() { |
| 244 |
return std::vector<int>().begin(); // expected-warning {{returning address of local temporary object}} |
| 245 |
} |
| 246 |
|
| 247 |
const int *modelFreeFunctions() { |
| 248 |
return std::data(std::vector<int>()); // expected-warning {{returning address of local temporary object}} |
| 249 |
} |
| 250 |
|
| 251 |
int &modelAnyCast() { |
| 252 |
return std::any_cast<int&>(std::any{}); // expected-warning {{returning reference to local temporary object}} |
| 253 |
} |
| 254 |
|
| 255 |
int modelAnyCast2() { |
| 256 |
return std::any_cast<int>(std::any{}); // ok |
| 257 |
} |
| 258 |
|
| 259 |
int modelAnyCast3() { |
| 260 |
return std::any_cast<int&>(std::any{}); // ok |
| 261 |
} |
| 262 |
|
| 263 |
const char *danglingRawPtrFromLocal() { |
| 264 |
std::basic_string<char> s; |
| 265 |
return s.c_str(); // expected-warning {{address of stack memory associated with local variable 's' returned}} |
| 266 |
} |
| 267 |
|
| 268 |
int &danglingRawPtrFromLocal2() { |
| 269 |
std::optional<int> o; |
| 270 |
return o.value(); // expected-warning {{reference to stack memory associated with local variable 'o' returned}} |
| 271 |
} |
| 272 |
|
| 273 |
int &danglingRawPtrFromLocal3() { |
| 274 |
std::optional<int> o; |
| 275 |
return *o; // expected-warning {{reference to stack memory associated with local variable 'o' returned}} |
| 276 |
} |
| 277 |
|
| 278 |
const char *danglingRawPtrFromTemp() { |
| 279 |
return std::basic_string<char>().c_str(); // expected-warning {{returning address of local temporary object}} |
| 280 |
} |
| 281 |
|
| 282 |
std::unique_ptr<int> getUniquePtr(); |
| 283 |
|
| 284 |
int *danglingUniquePtrFromTemp() { |
| 285 |
return getUniquePtr().get(); // expected-warning {{returning address of local temporary object}} |
| 286 |
} |
| 287 |
|
| 288 |
int *danglingUniquePtrFromTemp2() { |
| 289 |
return std::unique_ptr<int>().get(); // expected-warning {{returning address of local temporary object}} |
| 290 |
} |
| 291 |
|
| 292 |
void danglingReferenceFromTempOwner() { |
| 293 |
int &&r = *std::optional<int>(); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} |
| 294 |
int &&r2 = *std::optional<int>(5); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} |
| 295 |
int &&r3 = std::optional<int>(5).value(); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} |
| 296 |
int &r4 = std::vector<int>().at(3); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} |
| 297 |
} |
| 298 |
|
| 299 |
std::vector<int> getTempVec(); |
| 300 |
std::optional<std::vector<int>> getTempOptVec(); |
| 301 |
|
| 302 |
void testLoops() { |
| 303 |
for (auto i : getTempVec()) // ok |
| 304 |
; |
| 305 |
for (auto i : *getTempOptVec()) // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} |
| 306 |
; |
| 307 |
} |
| 308 |
|
| 309 |
int &usedToBeFalsePositive(std::vector<int> &v) { |
| 310 |
std::vector<int>::iterator it = v.begin(); |
| 311 |
int& value = *it; |
| 312 |
return value; // ok |
| 313 |
} |
| 314 |
|
| 315 |
int &doNotFollowReferencesForLocalOwner() { |
| 316 |
std::unique_ptr<int> localOwner; |
| 317 |
int &p = *localOwner.get(); |
| 318 |
// In real world code localOwner is usually moved here. |
| 319 |
return p; // ok |
| 320 |
} |
| 321 |
|
| 322 |
const char *trackThroughMultiplePointer() { |
| 323 |
return std::basic_string_view<char>(std::basic_string<char>()).begin(); // expected-warning {{returning address of local temporary object}} |
| 324 |
} |
| 325 |
|
| 326 |
struct X { |
| 327 |
X(std::unique_ptr<int> up) : |
| 328 |
pointee(*up), pointee2(up.get()), pointer(std::move(up)) {} |
| 329 |
int &pointee; |
| 330 |
int *pointee2; |
| 331 |
std::unique_ptr<int> pointer; |
| 332 |
}; |
| 333 |
|
| 334 |
std::vector<int>::iterator getIt(); |
| 335 |
std::vector<int> getVec(); |
| 336 |
|
| 337 |
const int &handleGslPtrInitsThroughReference() { |
| 338 |
const auto &it = getIt(); // Ok, it is lifetime extended. |
| 339 |
return *it; |
| 340 |
} |
| 341 |
|
| 342 |
void handleGslPtrInitsThroughReference2() { |
| 343 |
const std::vector<int> &v = getVec(); |
| 344 |
const int *val = v.data(); // Ok, it is lifetime extended. |
| 345 |
} |
| 346 |
|
| 347 |
void handleTernaryOperator(bool cond) { |
| 348 |
std::basic_string<char> def; |
| 349 |
std::basic_string_view<char> v = cond ? def : ""; // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}} |
| 350 |
} |
| 351 |
|
| 352 |
std::string operator+(std::string_view s1, std::string_view s2); |
| 353 |
void danglingStringviewAssignment(std::string_view a1, std::string_view a2) { |
| 354 |
a1 = std::string(); // expected-warning {{object backing}} |
| 355 |
a2 = a1 + a1; // expected-warning {{object backing}} |
| 356 |
} |
| 357 |
|
| 358 |
std::reference_wrapper<int> danglingPtrFromNonOwnerLocal() { |
| 359 |
int i = 5; |
| 360 |
return i; // TODO |
| 361 |
} |
| 362 |
|
| 363 |
std::reference_wrapper<int> danglingPtrFromNonOwnerLocal2() { |
| 364 |
int i = 5; |
| 365 |
return std::ref(i); // TODO |
| 366 |
} |
| 367 |
|
| 368 |
std::reference_wrapper<int> danglingPtrFromNonOwnerLocal3() { |
| 369 |
int i = 5; |
| 370 |
return std::reference_wrapper<int>(i); // TODO |
| 371 |
} |
| 372 |
|
| 373 |
std::reference_wrapper<Unannotated> danglingPtrFromNonOwnerLocal4() { |
| 374 |
Unannotated i; |
| 375 |
return std::reference_wrapper<Unannotated>(i); // TODO |
| 376 |
} |
| 377 |
|
| 378 |
std::reference_wrapper<Unannotated> danglingPtrFromNonOwnerLocal5() { |
| 379 |
Unannotated i; |
| 380 |
return std::ref(i); // TODO |
| 381 |
} |
| 382 |
|
| 383 |
int *returnPtrToLocalArray() { |
| 384 |
int a[5]; |
| 385 |
return std::begin(a); // TODO |
| 386 |
} |
| 387 |
|
| 388 |
struct ptr_wrapper { |
| 389 |
std::vector<int>::iterator member; |
| 390 |
}; |
| 391 |
|
| 392 |
ptr_wrapper getPtrWrapper(); |
| 393 |
|
| 394 |
std::vector<int>::iterator returnPtrFromWrapper() { |
| 395 |
ptr_wrapper local = getPtrWrapper(); |
| 396 |
return local.member; |
| 397 |
} |
| 398 |
|
| 399 |
std::vector<int>::iterator returnPtrFromWrapperThroughRef() { |
| 400 |
ptr_wrapper local = getPtrWrapper(); |
| 401 |
ptr_wrapper &local2 = local; |
| 402 |
return local2.member; |
| 403 |
} |
| 404 |
|
| 405 |
std::vector<int>::iterator returnPtrFromWrapperThroughRef2() { |
| 406 |
ptr_wrapper local = getPtrWrapper(); |
| 407 |
std::vector<int>::iterator &local2 = local.member; |
| 408 |
return local2; |
| 409 |
} |
| 410 |
|
| 411 |
void checkPtrMemberFromAggregate() { |
| 412 |
std::vector<int>::iterator local = getPtrWrapper().member; // OK. |
| 413 |
} |
| 414 |
|
| 415 |
std::vector<int>::iterator doNotInterferWithUnannotated() { |
| 416 |
Unannotated value; |
| 417 |
// Conservative choice for now. Probably not ok, but we do not warn. |
| 418 |
return std::begin(value); |
| 419 |
} |
| 420 |
|
| 421 |
std::vector<int>::iterator doNotInterferWithUnannotated2() { |
| 422 |
Unannotated value; |
| 423 |
return value; |
| 424 |
} |
| 425 |
|
| 426 |
std::vector<int>::iterator supportDerefAddrofChain(int a, std::vector<int>::iterator value) { |
| 427 |
switch (a) { |
| 428 |
default: |
| 429 |
return value; |
| 430 |
case 1: |
| 431 |
return *&value; |
| 432 |
case 2: |
| 433 |
return *&*&value; |
| 434 |
case 3: |
| 435 |
return *&*&*&value; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
int &supportDerefAddrofChain2(int a, std::vector<int>::iterator value) { |
| 440 |
switch (a) { |
| 441 |
default: |
| 442 |
return *value; |
| 443 |
case 1: |
| 444 |
return **&value; |
| 445 |
case 2: |
| 446 |
return **&*&value; |
| 447 |
case 3: |
| 448 |
return **&*&*&value; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
int *supportDerefAddrofChain3(int a, std::vector<int>::iterator value) { |
| 453 |
switch (a) { |
| 454 |
default: |
| 455 |
return &*value; |
| 456 |
case 1: |
| 457 |
return &*&*value; |
| 458 |
case 2: |
| 459 |
return &*&**&value; |
| 460 |
case 3: |
| 461 |
return &*&**&*&value; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
MyIntPointer handleDerivedToBaseCast1(MySpecialIntPointer ptr) { |
| 466 |
return ptr; |
| 467 |
} |
| 468 |
|
| 469 |
MyIntPointer handleDerivedToBaseCast2(MyOwnerIntPointer ptr) { |
| 470 |
return ptr; // expected-warning {{address of stack memory associated with parameter 'ptr' returned}} |
| 471 |
} |
| 472 |
|
| 473 |
std::vector<int>::iterator noFalsePositiveWithVectorOfPointers() { |
| 474 |
std::vector<std::vector<int>::iterator> iters; |
| 475 |
return iters.at(0); |
| 476 |
} |
| 477 |
|
| 478 |
void testForBug49342() |
| 479 |
{ |
| 480 |
auto it = std::iter<char>{} - 2; // Used to be false positive. |
| 481 |
} |