159 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// Make sure that std::string_view's iterators check for OOB accesses when the debug mode is enabled.10 11// REQUIRES: has-unix-headers, libcpp-has-abi-bounded-iterators12// UNSUPPORTED: libcpp-hardening-mode=none13 14#include <iterator>15#include <string_view>16 17#include "check_assertion.h"18 19template <typename Iter>20void test_iterator(Iter begin, Iter end, bool reverse) {21 ptrdiff_t distance = std::distance(begin, end);22 23 // Dereferencing an iterator at the end.24 {25 TEST_LIBCPP_ASSERT_FAILURE(26 *end,27 reverse ? "__bounded_iter::operator--: Attempt to rewind an iterator past the start"28 : "__bounded_iter::operator*: Attempt to dereference an iterator at the end");29#if _LIBCPP_STD_VER >= 2030 // In C++20 mode, std::reverse_iterator implements operator->, but not operator*, with31 // std::prev instead of operator--. std::prev ultimately calls operator+32 TEST_LIBCPP_ASSERT_FAILURE(33 end.operator->(),34 reverse ? "__bounded_iter::operator+=: Attempt to rewind an iterator past the start"35 : "__bounded_iter::operator->: Attempt to dereference an iterator at the end");36#else37 TEST_LIBCPP_ASSERT_FAILURE(38 end.operator->(),39 reverse ? "__bounded_iter::operator--: Attempt to rewind an iterator past the start"40 : "__bounded_iter::operator->: Attempt to dereference an iterator at the end");41#endif42 }43 44 // Incrementing an iterator past the end.45 {46 [[maybe_unused]] const char* msg =47 reverse ? "__bounded_iter::operator--: Attempt to rewind an iterator past the start"48 : "__bounded_iter::operator++: Attempt to advance an iterator past the end";49 auto it = end;50 TEST_LIBCPP_ASSERT_FAILURE(it++, msg);51 it = end;52 TEST_LIBCPP_ASSERT_FAILURE(++it, msg);53 }54 55 // Decrementing an iterator past the start.56 {57 [[maybe_unused]] const char* msg =58 reverse ? "__bounded_iter::operator++: Attempt to advance an iterator past the end"59 : "__bounded_iter::operator--: Attempt to rewind an iterator past the start";60 auto it = begin;61 TEST_LIBCPP_ASSERT_FAILURE(it--, msg);62 it = begin;63 TEST_LIBCPP_ASSERT_FAILURE(--it, msg);64 }65 66 // Advancing past the end with operator+= and operator+.67 {68 [[maybe_unused]] const char* msg =69 reverse ? "__bounded_iter::operator-=: Attempt to rewind an iterator past the start"70 : "__bounded_iter::operator+=: Attempt to advance an iterator past the end";71 auto it = end;72 TEST_LIBCPP_ASSERT_FAILURE(it += 1, msg);73 TEST_LIBCPP_ASSERT_FAILURE(end + 1, msg);74 it = begin;75 TEST_LIBCPP_ASSERT_FAILURE(it += (distance + 1), msg);76 TEST_LIBCPP_ASSERT_FAILURE(begin + (distance + 1), msg);77 }78 79 // Advancing past the end with operator-= and operator-.80 {81 [[maybe_unused]] const char* msg =82 reverse ? "__bounded_iter::operator+=: Attempt to rewind an iterator past the start"83 : "__bounded_iter::operator-=: Attempt to advance an iterator past the end";84 auto it = end;85 TEST_LIBCPP_ASSERT_FAILURE(it -= (-1), msg);86 TEST_LIBCPP_ASSERT_FAILURE(end - (-1), msg);87 it = begin;88 TEST_LIBCPP_ASSERT_FAILURE(it -= (-distance - 1), msg);89 TEST_LIBCPP_ASSERT_FAILURE(begin - (-distance - 1), msg);90 }91 92 // Rewinding past the start with operator+= and operator+.93 {94 [[maybe_unused]] const char* msg =95 reverse ? "__bounded_iter::operator-=: Attempt to advance an iterator past the end"96 : "__bounded_iter::operator+=: Attempt to rewind an iterator past the start";97 auto it = begin;98 TEST_LIBCPP_ASSERT_FAILURE(it += (-1), msg);99 TEST_LIBCPP_ASSERT_FAILURE(begin + (-1), msg);100 it = end;101 TEST_LIBCPP_ASSERT_FAILURE(it += (-distance - 1), msg);102 TEST_LIBCPP_ASSERT_FAILURE(end + (-distance - 1), msg);103 }104 105 // Rewinding past the start with operator-= and operator-.106 {107 [[maybe_unused]] const char* msg =108 reverse ? "__bounded_iter::operator+=: Attempt to advance an iterator past the end"109 : "__bounded_iter::operator-=: Attempt to rewind an iterator past the start";110 auto it = begin;111 TEST_LIBCPP_ASSERT_FAILURE(it -= 1, msg);112 TEST_LIBCPP_ASSERT_FAILURE(begin - 1, msg);113 it = end;114 TEST_LIBCPP_ASSERT_FAILURE(it -= (distance + 1), msg);115 TEST_LIBCPP_ASSERT_FAILURE(end - (distance + 1), msg);116 }117 118 // Out-of-bounds operator[].119 {120 [[maybe_unused]] const char* end_msg =121 reverse ? "__bounded_iter::operator--: Attempt to rewind an iterator past the start"122 : "__bounded_iter::operator[]: Attempt to index an iterator at or past the end";123 [[maybe_unused]] const char* past_end_msg =124 reverse ? "__bounded_iter::operator-=: Attempt to rewind an iterator past the start"125 : "__bounded_iter::operator[]: Attempt to index an iterator at or past the end";126 [[maybe_unused]] const char* past_start_msg =127 reverse ? "__bounded_iter::operator-=: Attempt to advance an iterator past the end"128 : "__bounded_iter::operator[]: Attempt to index an iterator past the start";129 TEST_LIBCPP_ASSERT_FAILURE(begin[distance], end_msg);130 TEST_LIBCPP_ASSERT_FAILURE(begin[distance + 1], past_end_msg);131 TEST_LIBCPP_ASSERT_FAILURE(begin[-1], past_start_msg);132 TEST_LIBCPP_ASSERT_FAILURE(begin[-99], past_start_msg);133 134 auto it = begin + 1;135 TEST_LIBCPP_ASSERT_FAILURE(it[distance - 1], end_msg);136 TEST_LIBCPP_ASSERT_FAILURE(it[distance], past_end_msg);137 TEST_LIBCPP_ASSERT_FAILURE(it[-2], past_start_msg);138 TEST_LIBCPP_ASSERT_FAILURE(it[-99], past_start_msg);139 }140}141 142int main(int, char**) {143 std::string_view const str("hello world");144 145 // string_view::iterator146 test_iterator(str.begin(), str.end(), /*reverse=*/false);147 148 // string_view::const_iterator149 test_iterator(str.cbegin(), str.cend(), /*reverse=*/false);150 151 // string_view::reverse_iterator152 test_iterator(str.rbegin(), str.rend(), /*reverse=*/true);153 154 // string_view::const_reverse_iterator155 test_iterator(str.crbegin(), str.crend(), /*reverse=*/true);156 157 return 0;158}159