From 0111975c07233512e63371b2bc99f88d9670bbc9 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Mon, 1 Nov 2021 11:06:51 +0000 Subject: [PATCH] libstdc++: Fix range access for empty std::valarray [PR103022] The std::begin and std::end overloads for std::valarray are defined in terms of std::addressof(v[0]) which is undefined for an empty valarray. libstdc++-v3/ChangeLog: PR libstdc++/103022 * include/std/valarray (begin, end): Do not dereference an empty valarray. Add noexcept and [[nodiscard]]. * testsuite/26_numerics/valarray/range_access.cc: Check empty valarray. Check iterator properties. Run as well as compiling. * testsuite/26_numerics/valarray/range_access2.cc: Likewise. * testsuite/26_numerics/valarray/103022.cc: New test. (cherry picked from commit 91bac9fed5d082f0b180834110ebc0f46f97599a) --- libstdc++-v3/include/std/valarray | 30 +++++++++--- .../testsuite/26_numerics/valarray/103022.cc | 15 ++++++ .../26_numerics/valarray/range_access.cc | 49 ++++++++++++++++--- .../26_numerics/valarray/range_access2.cc | 22 ++++++++- 4 files changed, 100 insertions(+), 16 deletions(-) create mode 100644 libstdc++-v3/testsuite/26_numerics/valarray/103022.cc diff --git a/libstdc++-v3/include/std/valarray b/libstdc++-v3/include/std/valarray index ad3e14ebe52..72eb7a9d56d 100644 --- a/libstdc++-v3/include/std/valarray +++ b/libstdc++-v3/include/std/valarray @@ -1210,9 +1210,10 @@ _DEFINE_BINARY_OPERATOR(>=, __greater_equal) * @param __va valarray. */ template + [[__nodiscard__]] inline _Tp* - begin(valarray<_Tp>& __va) - { return std::__addressof(__va[0]); } + begin(valarray<_Tp>& __va) noexcept + { return __va.size() ? std::__addressof(__va[0]) : nullptr; } /** * @brief Return an iterator pointing to the first element of @@ -1220,9 +1221,10 @@ _DEFINE_BINARY_OPERATOR(>=, __greater_equal) * @param __va valarray. */ template + [[__nodiscard__]] inline const _Tp* - begin(const valarray<_Tp>& __va) - { return std::__addressof(__va[0]); } + begin(const valarray<_Tp>& __va) noexcept + { return __va.size() ? std::__addressof(__va[0]) : nullptr; } /** * @brief Return an iterator pointing to one past the last element of @@ -1230,9 +1232,15 @@ _DEFINE_BINARY_OPERATOR(>=, __greater_equal) * @param __va valarray. */ template + [[__nodiscard__]] inline _Tp* - end(valarray<_Tp>& __va) - { return std::__addressof(__va[0]) + __va.size(); } + end(valarray<_Tp>& __va) noexcept + { + if (auto __n = __va.size()) + return std::__addressof(__va[0]) + __n; + else + return nullptr; + } /** * @brief Return an iterator pointing to one past the last element of @@ -1240,9 +1248,15 @@ _DEFINE_BINARY_OPERATOR(>=, __greater_equal) * @param __va valarray. */ template + [[__nodiscard__]] inline const _Tp* - end(const valarray<_Tp>& __va) - { return std::__addressof(__va[0]) + __va.size(); } + end(const valarray<_Tp>& __va) noexcept + { + if (auto __n = __va.size()) + return std::__addressof(__va[0]) + __n; + else + return nullptr; + } #endif // C++11 /// @} group numeric_arrays diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/103022.cc b/libstdc++-v3/testsuite/26_numerics/valarray/103022.cc new file mode 100644 index 00000000000..d2e346760dd --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/valarray/103022.cc @@ -0,0 +1,15 @@ +// { dg-options "-D_GLIBCXX_DEBUG" } +// { dg-do compile { target c++11 } } + +#include + +int main() +{ + // PR libstdc++/103022 + std::valarray va; + (void) std::begin(va); + (void) std::end(va); + const auto& cva = va; + (void) std::begin(cva); + (void) std::end(cva); +} diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/range_access.cc b/libstdc++-v3/testsuite/26_numerics/valarray/range_access.cc index c015d1890b6..c49c2c52f47 100644 --- a/libstdc++-v3/testsuite/26_numerics/valarray/range_access.cc +++ b/libstdc++-v3/testsuite/26_numerics/valarray/range_access.cc @@ -1,4 +1,4 @@ -// { dg-do compile { target c++11 } } +// { dg-do run { target c++11 } } // Copyright (C) 2010-2021 Free Software Foundation, Inc. // @@ -17,7 +17,7 @@ // with this library; see the file COPYING3. If not see // . -// 26.6.10 valarray range access: [valarray.range] +// C++11 26.6.10 valarray range access: [valarray.range] #include @@ -25,9 +25,46 @@ void test01() { std::valarray va{1.0, 2.0, 3.0}; - std::begin(va); - std::end(va); + (void) std::begin(va); + (void) std::end(va); const auto& cva = va; - std::begin(cva); - std::end(cva); + (void) std::begin(cva); + (void) std::end(cva); + + using Iter = decltype(std::begin(va)); + using IterTraits = std::iterator_traits; + static_assert( std::is_same::value, "" ); + static_assert( std::is_same::value, "" ); + static_assert( std::is_same::value, "" ); + static_assert( std::is_same::value, "" ); + using CIter = decltype(std::begin(cva)); + using CIterTraits = std::iterator_traits; + static_assert( std::is_same::value, "" ); + static_assert( std::is_same::value, "" ); + static_assert( std::is_same::value, "" ); + static_assert( std::is_same::value, "" ); +#if __cplusplus >= 202002L + static_assert( std::contiguous_iterator ); + static_assert( std::contiguous_iterator ); +#endif +} + +// PR libstdc++/103022 +void +test02() +{ + std::valarray va; + (void) std::begin(va); + (void) std::end(va); + const auto& cva = va; + (void) std::begin(cva); + (void) std::end(cva); +} + +int main() +{ + test01(); + test02(); } diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/range_access2.cc b/libstdc++-v3/testsuite/26_numerics/valarray/range_access2.cc index a7ce1332117..46cfd29e5b2 100644 --- a/libstdc++-v3/testsuite/26_numerics/valarray/range_access2.cc +++ b/libstdc++-v3/testsuite/26_numerics/valarray/range_access2.cc @@ -1,4 +1,4 @@ -// { dg-do compile { target c++14 } } +// { dg-do run { target c++14 } } // Copyright (C) 2015-2021 Free Software Foundation, Inc. // @@ -17,7 +17,7 @@ // with this library; see the file COPYING3. If not see // . -// 26.6.10 valarray range access: [valarray.range] +// C++11 26.6.10 valarray range access: [valarray.range] #include #include @@ -33,3 +33,21 @@ test01() std::cbegin(cva); std::cend(cva); } + +// PR libstdc++/103022 +void +test02() +{ + std::valarray va; + (void) std::cbegin(va); + (void) std::cend(va); + const auto& cva = va; + (void) std::cbegin(cva); + (void) std::cend(cva); +} + +int main() +{ + test01(); + test02(); +}