Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion libcxx/include/span
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ public:
_LIBCPP_HIDE_FROM_ABI constexpr explicit span(_It __first, size_type __count) : __data_{std::to_address(__first)} {
(void)__count;
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(_Extent == __count, "size mismatch in span's constructor (iterator, len)");
_LIBCPP_ASSERT_VALID_INPUT_RANGE(__count == 0 || std::to_address(__first) != nullptr,
"passed nullptr with non-zero length in span's constructor (iterator, len)");
}

template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
Expand Down Expand Up @@ -441,7 +443,10 @@ public:

template <__span_compatible_iterator<element_type> _It>
_LIBCPP_HIDE_FROM_ABI constexpr span(_It __first, size_type __count)
: __data_{std::to_address(__first)}, __size_{__count} {}
: __data_{std::to_address(__first)}, __size_{__count} {
_LIBCPP_ASSERT_VALID_INPUT_RANGE(__count == 0 || std::to_address(__first) != nullptr,
"passed nullptr with non-zero length in span's constructor (iterator, len)");
}

template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
_LIBCPP_HIDE_FROM_ABI constexpr span(_It __first, _End __last)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,48 @@
#include "check_assertion.h"

int main(int, char**) {
std::array<int, 3> array{0, 1, 2};
std::array<int, 3> array{0, 1, 2};

auto too_large = [&] { std::span<int, 3> const s(array.data(), 4); (void)s; };
TEST_LIBCPP_ASSERT_FAILURE(too_large(), "size mismatch in span's constructor (iterator, len)");
// Input range too large (exceeds the span extent)
{
auto f = [&] {
std::span<int, 3> const s(array.data(), 4);
(void)s;
};
TEST_LIBCPP_ASSERT_FAILURE(f(), "size mismatch in span's constructor (iterator, len)");
}

auto too_small = [&] { std::span<int, 3> const s(array.data(), 2); (void)s; };
TEST_LIBCPP_ASSERT_FAILURE(too_small(), "size mismatch in span's constructor (iterator, len)");
// Input range too small (doesn't fill the span)
{
auto f = [&] {
std::span<int, 3> const s(array.data(), 2);
(void)s;
};
TEST_LIBCPP_ASSERT_FAILURE(f(), "size mismatch in span's constructor (iterator, len)");
}

return 0;
// Input range is non-empty but starts with a null pointer
{
// static extent
{
auto f = [&] {
int* p = nullptr;
std::span<int, 3> const s(p, 3);
(void)s;
};
TEST_LIBCPP_ASSERT_FAILURE(f(), "passed nullptr with non-zero length in span's constructor (iterator, len)");
}

// dynamic extent
{
auto f = [&] {
int* p = nullptr;
std::span<int, std::dynamic_extent> const s(p, 1);
(void)s;
};
TEST_LIBCPP_ASSERT_FAILURE(f(), "passed nullptr with non-zero length in span's constructor (iterator, len)");
}
}

return 0;
}
Loading