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 |
18 | // REQUIRES: libcpp-has-abi-bounded-iterators-in-vector |
19 | // UNSUPPORTED: c++03 |
20 | // UNSUPPORTED: libcpp-hardening-mode=none |
21 | // XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing |
22 | |
23 | #include <vector> |
24 | #include <cassert> |
25 | #include <cstddef> |
26 | |
27 | #include "check_assertion.h" |
28 | |
29 | int main(int, char**) { |
30 | std::vector<int> vec; |
31 | vec.reserve(4); |
32 | std::size_t old_capacity = vec.capacity(); |
33 | assert(old_capacity >= 4); |
34 | |
35 | vec.push_back(0); |
36 | vec.push_back(1); |
37 | vec.push_back(2); |
38 | auto it = vec.begin(); |
39 | vec.push_back(3); |
40 | assert(vec.capacity() == old_capacity); |
41 | |
42 | // The capacity did not change, so the iterator remains valid and can reach the new element. |
43 | assert(*it == 0); |
44 | assert(*(it + 1) == 1); |
45 | assert(*(it + 2) == 2); |
46 | assert(*(it + 3) == 3); |
47 | |
48 | while (vec.capacity() == old_capacity) { |
49 | vec.push_back(42); |
50 | } |
51 | TEST_LIBCPP_ASSERT_FAILURE( |
52 | *(it + old_capacity), "__bounded_iter::operator*: Attempt to dereference an iterator at the end" ); |
53 | // Unfortunately, the bounded iterator does not detect that it's been invalidated and will still allow attempts to |
54 | // dereference elements 0 to 3 (even though they refer to memory that's been reallocated). |
55 | |
56 | return 0; |
57 | } |
58 | |