1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // <vector> 10 11 // void push_back(const value_type& x); 12 // 13 // If no reallocation happens, then references, pointers, and iterators before 14 // the insertion point remain valid but those at or after the insertion point, 15 // including the past-the-end iterator, are invalidated. 16 17 // REQUIRES: has-unix-headers, libcpp-has-abi-bounded-iterators-in-vector 18 // UNSUPPORTED: c++03 19 // UNSUPPORTED: libcpp-hardening-mode=none 20 // XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing 21 22 #include <vector> 23 #include <cassert> 24 #include <cstddef> 25 26 #include "check_assertion.h" 27 28 int main(int, char**) { 29 std::vector<int> vec; 30 vec.reserve(4); 31 std::size_t old_capacity = vec.capacity(); 32 assert(old_capacity >= 4); 33 34 vec.push_back(0); 35 vec.push_back(1); 36 vec.push_back(2); 37 auto it = vec.begin(); 38 vec.push_back(3); 39 assert(vec.capacity() == old_capacity); 40 41 // The capacity did not change, so the iterator remains valid and can reach the new element. 42 assert(*it == 0); 43 assert(*(it + 1) == 1); 44 assert(*(it + 2) == 2); 45 assert(*(it + 3) == 3); 46 47 while (vec.capacity() == old_capacity) { 48 vec.push_back(42); 49 } 50 TEST_LIBCPP_ASSERT_FAILURE( 51 *(it + old_capacity), "__bounded_iter::operator*: Attempt to dereference an iterator at the end"); 52 // Unfortunately, the bounded iterator does not detect that it's been invalidated and will still allow attempts to 53 // dereference elements 0 to 3 (even though they refer to memory that's been reallocated). 54 55 return 0; 56 } 57