summaryrefslogtreecommitdiff
path: root/libstdc++-v3
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2021-08-31 17:34:51 +0100
committerJonathan Wakely <jwakely@redhat.com>2021-11-24 11:24:58 +0000
commit238fd13380838ad57d199f3e9ca1d5403f33d381 (patch)
treed2dbafcddf7d4e4fd9a27763eb624ca8fbc7ff20 /libstdc++-v3
parent5709564895f38d7d1f76e6298514ed242f676bb8 (diff)
downloadgcc-238fd13380838ad57d199f3e9ca1d5403f33d381.tar.gz
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 ef7becc9c8a48804d3fd9dac032f7b33e561a612)
Diffstat (limited to 'libstdc++-v3')
-rw-r--r--libstdc++-v3/include/std/span2
-rw-r--r--libstdc++-v3/testsuite/23_containers/span/cons_1_assert_neg.cc14
-rw-r--r--libstdc++-v3/testsuite/23_containers/span/cons_2_assert_neg.cc14
3 files changed, 30 insertions, 0 deletions
diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span
index 86fe042e724..503b71fc8bd 100644
--- a/libstdc++-v3/include/std/span
+++ b/libstdc++-v3/include/std/span
@@ -160,6 +160,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>
@@ -175,6 +176,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
__glibcxx_assert((__last - __first) == _Extent);
}
+ __glibcxx_requires_valid_range(__first, __last);
}
template<size_t _ArrayExtent>
diff --git a/libstdc++-v3/testsuite/23_containers/span/cons_1_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/cons_1_assert_neg.cc
new file mode 100644
index 00000000000..2f555125453
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/span/cons_1_assert_neg.cc
@@ -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);
+}
diff --git a/libstdc++-v3/testsuite/23_containers/span/cons_2_assert_neg.cc b/libstdc++-v3/testsuite/23_containers/span/cons_2_assert_neg.cc
new file mode 100644
index 00000000000..efef0e608ba
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/span/cons_2_assert_neg.cc
@@ -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());
+}