[libcxx-commits] [libcxx] [libc++] Add missing assertion in std::span constructor (PR #118396)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Dec 2 13:07:57 PST 2024
https://github.com/ldionne created https://github.com/llvm/llvm-project/pull/118396
The (iterator, size) constructor should ensure that it gets passed a valid range when the size is not 0.
Fixes #107789
>From f4d499aecf3bef5f695d87716fefcc9cd3b2108e Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Mon, 2 Dec 2024 16:06:51 -0500
Subject: [PATCH] [libc++] Add missing assertion in std::span constructor
The (iterator, size) constructor should ensure that it gets
passed a valid range when the size is not 0.
Fixes #107789
---
libcxx/include/span | 7 ++-
.../span.cons/assert.iter_size.pass.cpp | 47 ++++++++++++++++---
2 files changed, 47 insertions(+), 7 deletions(-)
diff --git a/libcxx/include/span b/libcxx/include/span
index 896a3cd890186c..24ccc9be778764 100644
--- a/libcxx/include/span
+++ b/libcxx/include/span
@@ -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 ? true : 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>
@@ -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 ? true : 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)
diff --git a/libcxx/test/libcxx/containers/views/views.span/span.cons/assert.iter_size.pass.cpp b/libcxx/test/libcxx/containers/views/views.span/span.cons/assert.iter_size.pass.cpp
index 4461bad8ff5047..522bde13a5688c 100644
--- a/libcxx/test/libcxx/containers/views/views.span/span.cons/assert.iter_size.pass.cpp
+++ b/libcxx/test/libcxx/containers/views/views.span/span.cons/assert.iter_size.pass.cpp
@@ -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)");
+ // Providing too large value in constructor
+ {
+ 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)");
+ // Providing too small value in constructor
+ {
+ 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;
+ // Providing nullptr with a non-zero size in construction
+ {
+ // 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;
}
More information about the libcxx-commits
mailing list