[committed] libstdc++: Fix range access for empty std::valarray [PR103022]

Jonathan Wakely jwakely@redhat.com
Mon Nov 1 13:27:32 GMT 2021


Tested powerpc64le-linux, pushed to trunk.


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.
---
 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 5adc94282ee..c6242eb4db9 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<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
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 <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);
+}
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
 // <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();
 }
diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/range_access2.cc b/libstdc++-v3/testsuite/26_numerics/valarray/range_access2.cc
index cf5418a61d1..4217367cf22 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
 // <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()
   (void) std::cbegin(cva);
   (void) 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();
+}
-- 
2.31.1



More information about the Gcc-patches mailing list