You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
push_back, emplace_back | If the vector changed capacity, all of them. If not, only end().
Demonstrate:
autof()
{
std::vector<int> v{1, 2, 3, 4};
int &p = v.front();
std::cout << std::format("int&: {}\n", p);
v.push_back(5); // Triggers reallocation when changing capacity// p was potentially invalidated, using it can be use-after-free
std::cout << std::format("int&: {}\n", p);
}
The above code snippet doesn't trigger any warning with clang-tidy. However, it's captured by AddressSanitizer:
==15550==ERROR: AddressSanitizer: heap-use-after-free on address 0x602000000010 at pc 0x5636a3e859a5 bp 0x7ffd49ddf820 sp 0x7ffd49ddf810
C++ Core Guidelines should discourage the use of invalidated iterator and/or attempt to detect the use of dangling reference in compile-time.
The text was updated successfully, but these errors were encountered:
Editors call: We agree. This error is discouraged in ES.65 but that Guideline should explicitly mention that it applies to iterators as well (e.g., Note that by pointer here we mean any indirection to an object, such as an iterator or view). And this error is covered by the Lifetime profile (the live version of the doc in cplusplus/papers/#312 is this repo's Lifetime profile.
Not sure if this is the right place for this issue.
As per std::vector on cppreference, iterators are potentially invalidated on certain operations:
push_back, emplace_back | If the vector changed capacity, all of them. If not, only end().
Demonstrate:
The above code snippet doesn't trigger any warning with clang-tidy. However, it's captured by AddressSanitizer:
C++ Core Guidelines should discourage the use of invalidated iterator and/or attempt to detect the use of dangling reference in compile-time.
The text was updated successfully, but these errors were encountered: