]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Avoid reusing moved-from iterators in PSTL tests [PR90276]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 31 Jan 2024 10:41:49 +0000 (10:41 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 8 Feb 2024 16:12:04 +0000 (16:12 +0000)
The reverse_invoker utility for PSTL tests uses forwarding references for
all parameters, but some of those parameters get forwarded to move
constructors which then leave the objects in a moved-from state. When
the parameters are forwarded a second time that results in making new
copies of moved-from iterators.  For libstdc++ debug mode iterators, the
moved-from state is singular, which means copying them will abort at
runtime.

The fix is to make copies of iterator arguments instead of forwarding
them.

The callers of reverse_invoker::operator() also forward the iterators
multiple times, but that's OK because reverse_invoker accepts them by
forwarding reference but then breaks the chain of forwarding and copies
them as lvalues.

libstdc++-v3/ChangeLog:

PR libstdc++/90276
* testsuite/util/pstl/test_utils.h (reverse_invoker): Do not use
perfect forwarding for iterator arguments.

(cherry picked from commit 723a7c1ad29523b9ddff53c7b147bffea56fbb63)

libstdc++-v3/testsuite/util/pstl/test_utils.h

index 80a8f9c7b87903e87ea5101cf256af96754b6733..9991f37d7eee5280c1acc1ebe8f185b3b5428644 100644 (file)
@@ -1007,18 +1007,18 @@ struct iterator_invoker<std::forward_iterator_tag, /*isReverse=*/std::true_type>
 template <typename IsReverse>
 struct reverse_invoker
 {
-    template <typename... Rest>
+    template <typename Policy, typename Op, typename... Rest>
     void
-    operator()(Rest&&... rest)
+    operator()(Policy&& exec, Op op, Rest&&... rest)
     {
         // Random-access iterator
-        iterator_invoker<std::random_access_iterator_tag, IsReverse>()(std::forward<Rest>(rest)...);
+        iterator_invoker<std::random_access_iterator_tag, IsReverse>()(std::forward<Policy>(exec), op, rest...);
 
         // Forward iterator
-        iterator_invoker<std::forward_iterator_tag, IsReverse>()(std::forward<Rest>(rest)...);
+        iterator_invoker<std::forward_iterator_tag, IsReverse>()(std::forward<Policy>(exec), op, rest...);
 
         // Bidirectional iterator
-        iterator_invoker<std::bidirectional_iterator_tag, IsReverse>()(std::forward<Rest>(rest)...);
+        iterator_invoker<std::bidirectional_iterator_tag, IsReverse>()(std::forward<Policy>(exec), op, rest...);
     }
 };