Skip to content

Commit 9f471fd

Browse files
[libc++][hardening] Constrain construction for __{(static_)bounded,wrap}_iter (#115271)
This PR restricts construction to cases where reference types of source/destination iterators are (`T&`, `T&`) or (`T&`, `const T&`) ( where `T` can be const). Fixes #50058.
1 parent 3af4c2e commit 9f471fd

File tree

4 files changed

+96
-6
lines changed

4 files changed

+96
-6
lines changed

libcxx/include/__iterator/bounded_iter.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
#include <__config>
1717
#include <__iterator/iterator_traits.h>
1818
#include <__memory/pointer_traits.h>
19+
#include <__type_traits/conjunction.h>
20+
#include <__type_traits/disjunction.h>
1921
#include <__type_traits/enable_if.h>
2022
#include <__type_traits/integral_constant.h>
2123
#include <__type_traits/is_convertible.h>
24+
#include <__type_traits/is_same.h>
25+
#include <__type_traits/make_const_lvalue_ref.h>
2226
#include <__utility/move.h>
2327

2428
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -70,7 +74,12 @@ struct __bounded_iter {
7074
_LIBCPP_HIDE_FROM_ABI __bounded_iter(__bounded_iter const&) = default;
7175
_LIBCPP_HIDE_FROM_ABI __bounded_iter(__bounded_iter&&) = default;
7276

73-
template <class _OtherIterator, __enable_if_t< is_convertible<_OtherIterator, _Iterator>::value, int> = 0>
77+
template < class _OtherIterator,
78+
__enable_if_t<
79+
_And< is_convertible<const _OtherIterator&, _Iterator>,
80+
_Or<is_same<reference, __iter_reference<_OtherIterator> >,
81+
is_same<reference, __make_const_lvalue_ref<__iter_reference<_OtherIterator> > > > >::value,
82+
int> = 0>
7483
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bounded_iter(__bounded_iter<_OtherIterator> const& __other) _NOEXCEPT
7584
: __current_(__other.__current_),
7685
__begin_(__other.__begin_),

libcxx/include/__iterator/static_bounded_iter.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
#include <__cstddef/size_t.h>
1818
#include <__iterator/iterator_traits.h>
1919
#include <__memory/pointer_traits.h>
20+
#include <__type_traits/conjunction.h>
21+
#include <__type_traits/disjunction.h>
2022
#include <__type_traits/enable_if.h>
2123
#include <__type_traits/integral_constant.h>
2224
#include <__type_traits/is_convertible.h>
25+
#include <__type_traits/is_same.h>
26+
#include <__type_traits/make_const_lvalue_ref.h>
2327
#include <__utility/move.h>
2428

2529
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -93,7 +97,12 @@ struct __static_bounded_iter {
9397
_LIBCPP_HIDE_FROM_ABI __static_bounded_iter(__static_bounded_iter const&) = default;
9498
_LIBCPP_HIDE_FROM_ABI __static_bounded_iter(__static_bounded_iter&&) = default;
9599

96-
template <class _OtherIterator, __enable_if_t<is_convertible<_OtherIterator, _Iterator>::value, int> = 0>
100+
template <class _OtherIterator,
101+
__enable_if_t<
102+
_And< is_convertible<const _OtherIterator&, _Iterator>,
103+
_Or<is_same<reference, __iter_reference<_OtherIterator> >,
104+
is_same<reference, __make_const_lvalue_ref<__iter_reference<_OtherIterator> > > > >::value,
105+
int> = 0>
97106
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
98107
__static_bounded_iter(__static_bounded_iter<_OtherIterator, _Size> const& __other) _NOEXCEPT
99108
: __storage_(__other.__storage_.__current(), __other.__storage_.__begin()) {}
@@ -264,7 +273,7 @@ struct __static_bounded_iter {
264273
private:
265274
template <class>
266275
friend struct pointer_traits;
267-
template <class, size_t, class>
276+
template <class, size_t>
268277
friend struct __static_bounded_iter;
269278
__static_bounded_iter_storage<_Iterator, _Size> __storage_;
270279

libcxx/include/__iterator/wrap_iter.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
#include <__iterator/iterator_traits.h>
1818
#include <__memory/addressof.h>
1919
#include <__memory/pointer_traits.h>
20+
#include <__type_traits/conjunction.h>
21+
#include <__type_traits/disjunction.h>
2022
#include <__type_traits/enable_if.h>
2123
#include <__type_traits/integral_constant.h>
2224
#include <__type_traits/is_convertible.h>
25+
#include <__type_traits/is_same.h>
26+
#include <__type_traits/make_const_lvalue_ref.h>
2327

2428
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2529
# pragma GCC system_header
@@ -45,9 +49,14 @@ class __wrap_iter {
4549

4650
public:
4751
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter() _NOEXCEPT : __i_() {}
48-
template <class _Up, __enable_if_t<is_convertible<_Up, iterator_type>::value, int> = 0>
49-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter(const __wrap_iter<_Up>& __u) _NOEXCEPT
50-
: __i_(__u.base()) {}
52+
template <
53+
class _OtherIter,
54+
__enable_if_t< _And< is_convertible<const _OtherIter&, _Iter>,
55+
_Or<is_same<reference, __iter_reference<_OtherIter> >,
56+
is_same<reference, __make_const_lvalue_ref<__iter_reference<_OtherIter> > > > >::value,
57+
int> = 0>
58+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter(const __wrap_iter<_OtherIter>& __u) _NOEXCEPT
59+
: __i_(__u.__i_) {}
5160
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT { return *__i_; }
5261
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {
5362
return std::__to_address(__i_);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
10+
// <iterator>
11+
12+
// __bounded_iter<_Iter>
13+
// __static_bounded_iter<_Iter>
14+
// __wrap_iter<_Iter>
15+
16+
// Verify that libc++-wrapped iterators do not permit slicing conversion or construction.
17+
18+
#include <array>
19+
#include <span>
20+
#include <type_traits>
21+
#include <vector>
22+
23+
#include "test_macros.h"
24+
25+
struct Base {};
26+
struct Derived : Base {};
27+
28+
template <class B, class D, bool = std::is_pointer<typename std::array<B, 1>::iterator>::value>
29+
struct test_array_helper : std::true_type {
30+
typedef typename std::array<B, 1>::iterator BaseIter;
31+
typedef typename std::array<D, 1>::iterator DerivedIter;
32+
typedef typename std::array<B, 1>::const_iterator BaseConstIter;
33+
typedef typename std::array<D, 1>::const_iterator DerivedConstIter;
34+
35+
static_assert(!std::is_convertible<DerivedIter, BaseIter>::value, "");
36+
static_assert(!std::is_convertible<DerivedIter, BaseConstIter>::value, "");
37+
static_assert(!std::is_convertible<DerivedConstIter, BaseConstIter>::value, "");
38+
static_assert(!std::is_constructible<BaseIter, DerivedIter>::value, "");
39+
static_assert(!std::is_constructible<BaseConstIter, DerivedIter>::value, "");
40+
static_assert(!std::is_constructible<BaseConstIter, DerivedConstIter>::value, "");
41+
};
42+
43+
template <class B, class D>
44+
struct test_array_helper<B, D, true> : std::true_type {};
45+
46+
static_assert(test_array_helper<Base, Derived>::value, "");
47+
48+
static_assert(!std::is_convertible<std::vector<Derived>::iterator, std::vector<Base>::iterator>::value, "");
49+
static_assert(!std::is_convertible<std::vector<Derived>::iterator, std::vector<Base>::const_iterator>::value, "");
50+
static_assert(!std::is_convertible<std::vector<Derived>::const_iterator, std::vector<Base>::const_iterator>::value, "");
51+
static_assert(!std::is_constructible<std::vector<Base>::iterator, std::vector<Derived>::iterator>::value, "");
52+
static_assert(!std::is_constructible<std::vector<Base>::const_iterator, std::vector<Derived>::iterator>::value, "");
53+
static_assert(!std::is_constructible<std::vector<Base>::const_iterator, std::vector<Derived>::const_iterator>::value,
54+
"");
55+
56+
#if TEST_STD_VER >= 20
57+
static_assert(!std::is_convertible_v<std::span<Derived>::iterator, std::span<Base>::iterator>);
58+
static_assert(!std::is_convertible_v<std::span<Derived>::iterator, std::span<const Base>::iterator>);
59+
static_assert(!std::is_convertible_v<std::span<const Derived>::iterator, std::span<Base>::iterator>);
60+
static_assert(!std::is_constructible_v<std::span<Base>::iterator, std::span<Derived>::iterator>);
61+
static_assert(!std::is_constructible_v<std::span<Base>::iterator, std::span<const Derived>::iterator>);
62+
static_assert(!std::is_constructible_v<std::span<const Base>::iterator, std::span<const Derived>::iterator>);
63+
#endif

0 commit comments

Comments
 (0)