libstdc++: Add valid range checks to std::span constructors [PR98421]

Signed-off-by: Jonathan Wakely <jwakely@redhat.com>

libstdc++-v3/ChangeLog:

	PR libstdc++/98421
	* include/std/span (span(Iter, size_type), span(Iter, Iter)):
	Add valid range checks.
	* testsuite/23_containers/span/cons_1_assert_neg.cc: New test.
	* testsuite/23_containers/span/cons_2_assert_neg.cc: New test.

(cherry picked from commit ef7becc9c8)
This commit is contained in:
Jonathan Wakely 2021-08-31 17:34:51 +01:00
parent f05b125a8d
commit de802e4736
3 changed files with 30 additions and 0 deletions

View File

@ -165,6 +165,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
__glibcxx_assert(__count == _Extent);
}
__glibcxx_requires_valid_range(__first, __first + __count);
}
template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
@ -180,6 +181,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
__glibcxx_assert((__last - __first) == _Extent);
}
__glibcxx_requires_valid_range(__first, __last);
}
template<size_t _ArrayExtent>

View File

@ -0,0 +1,14 @@
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail *-*-* } }
// { dg-require-effective-target c++2a }
#undef _GLIBCXX_DEBUG
#define _GLIBCXX_DEBUG
#include <span>
#include <vector>
int main()
{
std::vector<int> v(2);
std::span<int, std::dynamic_extent> s(v.begin(), 3);
}

View File

@ -0,0 +1,14 @@
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail *-*-* } }
// { dg-require-effective-target c++2a }
#undef _GLIBCXX_DEBUG
#define _GLIBCXX_DEBUG
#include <span>
#include <vector>
int main()
{
std::vector<int> v(2), w(1);
std::span<int, std::dynamic_extent> s(v.begin(), w.end());
}