Skip to content

Commit 1645d99

Browse files
[libc++][hardening] Use static_assert for __(static_)bounded_iter (llvm#115304)
We can't `static_assert` `__libcpp_is_contiguous_iterator` for `__wrap_iter` currently because `__wrap_iter` is also used for wrapping user-defined fancy pointers. Fixes llvm#115002.
1 parent f7bb129 commit 1645d99

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

libcxx/include/__iterator/bounded_iter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ _LIBCPP_BEGIN_NAMESPACE_STD
4747
// pointer, it is undefined at the language level (see [expr.add]). If
4848
// bounded iterators exhibited this undefined behavior, we risk compiler
4949
// optimizations deleting non-redundant bounds checks.
50-
template <class _Iterator, class = __enable_if_t< __libcpp_is_contiguous_iterator<_Iterator>::value > >
50+
template <class _Iterator>
5151
struct __bounded_iter {
52+
static_assert(__libcpp_is_contiguous_iterator<_Iterator>::value,
53+
"Only contiguous iterators can be adapted by __bounded_iter.");
54+
5255
using value_type = typename iterator_traits<_Iterator>::value_type;
5356
using difference_type = typename iterator_traits<_Iterator>::difference_type;
5457
using pointer = typename iterator_traits<_Iterator>::pointer;
@@ -247,7 +250,7 @@ struct __bounded_iter {
247250
private:
248251
template <class>
249252
friend struct pointer_traits;
250-
template <class, class>
253+
template <class>
251254
friend struct __bounded_iter;
252255
_Iterator __current_; // current iterator
253256
_Iterator __begin_, __end_; // valid range represented as [begin, end]

libcxx/include/__iterator/static_bounded_iter.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ struct __static_bounded_iter_storage<_Iterator, 0> {
7070
// it can be computed from the start of the range.
7171
//
7272
// The operations on which this iterator wrapper traps are the same as `__bounded_iter`.
73-
template <class _Iterator, size_t _Size, class = __enable_if_t<__libcpp_is_contiguous_iterator<_Iterator>::value> >
73+
template <class _Iterator, size_t _Size>
7474
struct __static_bounded_iter {
75+
static_assert(__libcpp_is_contiguous_iterator<_Iterator>::value,
76+
"Only contiguous iterators can be adapted by __static_bounded_iter.");
77+
7578
using value_type = typename iterator_traits<_Iterator>::value_type;
7679
using difference_type = typename iterator_traits<_Iterator>::difference_type;
7780
using pointer = typename iterator_traits<_Iterator>::pointer;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
14+
// Verify that __bounded_iter does not accept non-contiguous iterators as determined by __libcpp_is_contiguous_iterator.
15+
// static_assert should be used, see https://github.com/llvm/llvm-project/issues/115002.
16+
// __wrap_iter cannot be so handled because it may directly wrap user-defined fancy pointers in libc++'s vector.
17+
18+
#include <deque>
19+
#include <vector>
20+
#include <array>
21+
22+
// expected-error-re@*:* {{static assertion failed due to requirement {{.*}}Only contiguous iterators can be adapted by __bounded_iter.}}
23+
std::__bounded_iter<std::deque<int>::iterator> bounded_iter;
24+
// expected-error-re@*:* {{static assertion failed due to requirement {{.*}}Only contiguous iterators can be adapted by __static_bounded_iter.}}
25+
std::__static_bounded_iter<std::deque<int>::iterator, 42> statically_bounded_iter;

0 commit comments

Comments
 (0)