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 91bac9fed5)
This commit is contained in:
Jonathan Wakely 2021-11-01 11:06:51 +00:00
parent d0d8bd6378
commit 0111975c07
4 changed files with 100 additions and 16 deletions

View File

@ -1210,9 +1210,10 @@ _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
* @param __va valarray.
*/
template<class _Tp>
[[__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<class _Tp>
[[__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<class _Tp>
[[__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<class _Tp>
[[__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

View File

@ -0,0 +1,15 @@
// { dg-options "-D_GLIBCXX_DEBUG" }
// { dg-do compile { target c++11 } }
#include <valarray>
int main()
{
// PR libstdc++/103022
std::valarray<double> va;
(void) std::begin(va);
(void) std::end(va);
const auto& cva = va;
(void) std::begin(cva);
(void) std::end(cva);
}

View File

@ -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
// <http://www.gnu.org/licenses/>.
// 26.6.10 valarray range access: [valarray.range]
// C++11 26.6.10 valarray range access: [valarray.range]
#include <valarray>
@ -25,9 +25,46 @@ void
test01()
{
std::valarray<double> 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<Iter>;
static_assert( std::is_same<Iter, decltype(std::end(va))>::value, "" );
static_assert( std::is_same<IterTraits::iterator_category,
std::random_access_iterator_tag>::value, "" );
static_assert( std::is_same<IterTraits::value_type, double>::value, "" );
static_assert( std::is_same<IterTraits::reference, double&>::value, "" );
using CIter = decltype(std::begin(cva));
using CIterTraits = std::iterator_traits<CIter>;
static_assert( std::is_same<CIter, decltype(std::end(cva))>::value, "" );
static_assert( std::is_same<CIterTraits::iterator_category,
std::random_access_iterator_tag>::value, "" );
static_assert( std::is_same<CIterTraits::value_type, double>::value, "" );
static_assert( std::is_same<CIterTraits::reference, const double&>::value, "" );
#if __cplusplus >= 202002L
static_assert( std::contiguous_iterator<Iter> );
static_assert( std::contiguous_iterator<CIter> );
#endif
}
// PR libstdc++/103022
void
test02()
{
std::valarray<double> 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();
}

View File

@ -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
// <http://www.gnu.org/licenses/>.
// 26.6.10 valarray range access: [valarray.range]
// C++11 26.6.10 valarray range access: [valarray.range]
#include <iterator>
#include <valarray>
@ -33,3 +33,21 @@ test01()
std::cbegin(cva);
std::cend(cva);
}
// PR libstdc++/103022
void
test02()
{
std::valarray<double> 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();
}