From cfc9fa3bdddc1af59b7854937b99516067fd8c63 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Tue, 18 Jun 2024 20:57:13 +0100 Subject: [PATCH] libstdc++: Enable more debug assertions during constant evaluation [PR111250] Some of our debug assertions expand to nothing unless _GLIBCXX_ASSERTIONS is defined, which means they are not checked during constant evaluation. By making them unconditionally expand to a __glibcxx_assert expression they will be checked during constant evaluation. This allows us to diagnose more instances of undefined behaviour at compile-time, such as accessing a vector past-the-end. libstdc++-v3/ChangeLog: PR libstdc++/111250 * include/debug/assertions.h (__glibcxx_requires_non_empty_range) (__glibcxx_requires_nonempty, __glibcxx_requires_subscript): Define to __glibcxx_assert expressions or to debug mode __glibcxx_check_xxx expressions. * testsuite/23_containers/array/element_access/constexpr_c++17.cc: Add checks for out-of-bounds accesses in constant expressions. * testsuite/23_containers/vector/element_access/constexpr.cc: Likewise. --- libstdc++-v3/include/debug/assertions.h | 14 +++--- .../array/element_access/constexpr_c++17.cc | 44 +++++++++++++++++++ .../vector/element_access/constexpr.cc | 24 ++++++++-- 3 files changed, 72 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/include/debug/assertions.h b/libstdc++-v3/include/debug/assertions.h index fff1ae8def03..20441e33897d 100644 --- a/libstdc++-v3/include/debug/assertions.h +++ b/libstdc++-v3/include/debug/assertions.h @@ -31,12 +31,7 @@ #include -#ifndef _GLIBCXX_ASSERTIONS -# define __glibcxx_requires_non_empty_range(_First,_Last) -# define __glibcxx_requires_nonempty() -# define __glibcxx_requires_subscript(_N) -#else - +#ifndef _GLIBCXX_DEBUG // Verify that [_First, _Last) forms a non-empty iterator range. # define __glibcxx_requires_non_empty_range(_First,_Last) \ __glibcxx_assert(_First != _Last) @@ -45,6 +40,13 @@ // Verify that the container is nonempty # define __glibcxx_requires_nonempty() \ __glibcxx_assert(!this->empty()) +#else // Use the more verbose Debug Mode checks. +# define __glibcxx_requires_non_empty_range(_First,_Last) \ + __glibcxx_check_non_empty_range(_First,_Last) +# define __glibcxx_requires_nonempty() \ + __glibcxx_check_nonempty() +# define __glibcxx_requires_subscript(_N) \ + __glibcxx_check_subscript(_N) #endif #if defined _GLIBCXX_DEBUG && _GLIBCXX_HOSTED diff --git a/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc b/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc index a14ad487b428..19ab1cc1f8e6 100644 --- a/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc +++ b/libstdc++-v3/testsuite/23_containers/array/element_access/constexpr_c++17.cc @@ -66,3 +66,47 @@ constexpr bool test_zero() } static_assert( test_zero() ); + +#ifdef __cpp_concepts +template + constexpr std::false_type + access_empty() { return {}; } + +template + requires (std::bool_constant<&std::array{}.at(0) != nullptr>::value) + constexpr std::true_type + access_empty() { return {}; } + +template + requires (std::bool_constant<&std::array{}[0] != nullptr>::value) + constexpr std::true_type + access_empty() { return {}; } + +template + requires (std::bool_constant<&std::array{}.front() != nullptr>::value) + constexpr std::true_type + access_empty() { return {}; } + +template + requires (std::bool_constant<&std::array{}.back() != nullptr>::value) + constexpr std::true_type + access_empty() { return {}; } + +static_assert( ! access_empty() ); + +template + constexpr std::false_type + access_past_the_end() { return {}; } + +template + requires (std::bool_constant{}.at(0) != nullptr>::value) + constexpr std::true_type + access_past_the_end() { return {}; } + +template + requires (std::bool_constant<&std::array{}[1] != nullptr>::value) + constexpr std::true_type + access_past_the_end() { return {}; } + +static_assert( ! access_past_the_end() ); +#endif diff --git a/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc b/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc index 19c91d28cd68..358ded47ad94 100644 --- a/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc +++ b/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc @@ -85,23 +85,39 @@ template access_empty() { return {}; } template - requires (std::bool_constant<(std::vector().at(0), true)>::value) + requires (std::bool_constant<&std::vector().at(0) != nullptr>::value) constexpr std::true_type access_empty() { return {}; } template - requires (std::bool_constant<(std::vector()[0], true)>::value) + requires (std::bool_constant<&std::vector()[0] != nullptr>::value) constexpr std::true_type access_empty() { return {}; } template - requires (std::bool_constant<(std::vector().front(), true)>::value) + requires (std::bool_constant<&std::vector().front() != nullptr>::value) constexpr std::true_type access_empty() { return {}; } template - requires (std::bool_constant<(std::vector().back(), true)>::value) + requires (std::bool_constant<&std::vector().back() != nullptr>::value) constexpr std::true_type access_empty() { return {}; } static_assert( ! access_empty() ); + +template + constexpr std::false_type + access_past_the_end() { return {}; } + +template + requires (std::bool_constant<&std::vector(3).at(3) != nullptr>::value) + constexpr std::true_type + access_past_the_end() { return {}; } + +template + requires (std::bool_constant<&std::vector(3)[3] != nullptr>::value) + constexpr std::true_type + access_past_the_end() { return {}; } + +static_assert( ! access_past_the_end() ); -- 2.47.2