Skip to content

Commit d761fe6

Browse files
ldionnetru
authored andcommitted
[libc++] Add a missing assertion in std::span's constructor
Also, add missing tests for assertions in span constructors. Now I believe that all of std::span's API should be hardened, and all the assertions should have a corresponding test. Differential Revision: https://reviews.llvm.org/D131681 (cherry picked from commit 8c6319e)
1 parent 12f27d8 commit d761fe6

File tree

5 files changed

+154
-3
lines changed

5 files changed

+154
-3
lines changed

libcxx/include/span

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,10 @@ public:
453453
: __data{_VSTD::to_address(__first)}, __size{__count} {}
454454

455455
template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
456-
_LIBCPP_INLINE_VISIBILITY
457-
constexpr span(_It __first, _End __last)
458-
: __data(_VSTD::to_address(__first)), __size(__last - __first) {}
456+
_LIBCPP_INLINE_VISIBILITY constexpr span(_It __first, _End __last)
457+
: __data(_VSTD::to_address(__first)), __size(__last - __first) {
458+
_LIBCPP_ASSERT(__last - __first >= 0, "invalid range in span's constructor (iterator, sentinel)");
459+
}
459460

460461
template <size_t _Sz>
461462
_LIBCPP_INLINE_VISIBILITY
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
// UNSUPPORTED: c++03, c++11, c++14, c++17
9+
10+
// <span>
11+
//
12+
// constexpr span<T, Extent>::span(Iterator it, Sentinel sent);
13+
//
14+
// Check that we ensure `Extent == sent - it` and also that `[it, sent)` is a valid range.
15+
//
16+
//
17+
// constexpr span<T, dynamic_extent>::span(Iterator it, Sentinel sent);
18+
//
19+
// Check that we ensure that `[it, sent)` is a valid range.
20+
21+
// REQUIRES: has-unix-headers
22+
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}}
23+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1
24+
25+
#include <array>
26+
#include <span>
27+
28+
#include "check_assertion.h"
29+
30+
int main(int, char**) {
31+
{
32+
std::array<int, 3> array{0, 1, 2};
33+
34+
auto invalid_range = [&] { std::span<int> const s(array.end(), array.begin()); (void)s; };
35+
TEST_LIBCPP_ASSERT_FAILURE(invalid_range(), "invalid range in span's constructor (iterator, sentinel)");
36+
}
37+
{
38+
std::array<int, 3> array{0, 1, 2};
39+
40+
auto invalid_range = [&] { std::span<int, 3> const s(array.end(), array.begin()); (void)s; };
41+
TEST_LIBCPP_ASSERT_FAILURE(invalid_range(), "invalid range in span's constructor (iterator, sentinel)");
42+
43+
auto invalid_size = [&] { std::span<int, 3> const s(array.begin(), array.begin() + 2); (void)s; };
44+
TEST_LIBCPP_ASSERT_FAILURE(invalid_size(), "invalid range in span's constructor (iterator, sentinel): last - first != extent");
45+
}
46+
47+
return 0;
48+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
// UNSUPPORTED: c++03, c++11, c++14, c++17
9+
10+
// <span>
11+
//
12+
// constexpr span<T, Extent>::span(Iterator, size_type);
13+
//
14+
// Check that the passed size is equal to the statically known extent.
15+
// Note that it doesn't make sense to validate the incoming size in the
16+
// dynamic_extent version.
17+
18+
// REQUIRES: has-unix-headers
19+
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}}
20+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1
21+
22+
#include <array>
23+
#include <span>
24+
25+
#include "check_assertion.h"
26+
27+
int main(int, char**) {
28+
std::array<int, 3> array{0, 1, 2};
29+
30+
auto too_large = [&] { std::span<int, 3> const s(array.data(), 4); (void)s; };
31+
TEST_LIBCPP_ASSERT_FAILURE(too_large(), "size mismatch in span's constructor (iterator, len)");
32+
33+
auto too_small = [&] { std::span<int, 3> const s(array.data(), 2); (void)s; };
34+
TEST_LIBCPP_ASSERT_FAILURE(too_small(), "size mismatch in span's constructor (iterator, len)");
35+
36+
return 0;
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
// UNSUPPORTED: c++03, c++11, c++14, c++17
9+
10+
// <span>
11+
//
12+
// constexpr span<T, Extent>::span(const span<U, dynamic_extent>& other);
13+
//
14+
// Check that we ensure `other.size() == Extent`.
15+
16+
// REQUIRES: has-unix-headers
17+
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}}
18+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1
19+
20+
#include <array>
21+
#include <span>
22+
23+
#include "check_assertion.h"
24+
25+
int main(int, char**) {
26+
std::array<int, 3> array{0, 1, 2};
27+
std::span<int> other(array.data(), 3);
28+
29+
auto invalid_source = [&] { std::span<int, 2> const s(other); (void)s; };
30+
TEST_LIBCPP_ASSERT_FAILURE(invalid_source(), "size mismatch in span's constructor (other span)");
31+
32+
return 0;
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
// UNSUPPORTED: c++03, c++11, c++14, c++17
9+
10+
// <span>
11+
//
12+
// constexpr span<T, Extent>::span(Range&& r);
13+
//
14+
// Check that we ensure `size(r) == Extent`.
15+
16+
// REQUIRES: has-unix-headers
17+
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}}
18+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1
19+
20+
#include <span>
21+
#include <vector>
22+
23+
#include "check_assertion.h"
24+
25+
int main(int, char**) {
26+
std::vector<int> vec{0, 1, 2}; // must use std::vector instead of std::array, because std::span has a special constructor from std::array
27+
28+
auto invalid_size = [&] { std::span<int, 2> const s(vec); (void)s; };
29+
TEST_LIBCPP_ASSERT_FAILURE(invalid_size(), "size mismatch in span's constructor (range)");
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)