[libcxx-commits] [pstl] [pstl] Avoid reusing moved-from iterators in PSTL tests (PR #80128)

Jonathan Wakely via libcxx-commits libcxx-commits at lists.llvm.org
Thu Feb 1 02:35:04 PST 2024


https://github.com/jwakely updated https://github.com/llvm/llvm-project/pull/80128

>From 18a237cf91bed8f4b097cceef72fb0fb4019c36c Mon Sep 17 00:00:00 2001
From: Jonathan Wakely <jwakely at redhat.com>
Date: Wed, 31 Jan 2024 10:47:53 +0000
Subject: [PATCH] [pstl] Avoid reusing moved-from iterators in PSTL tests

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. Doing this in reverse_invoker::operator() is sufficient to fix the
bug. It's OK to forward in invoke_on_all_iterator_types::operator() and
invoke_on_all_policies::operator() because those forwarded arguments do
not get copied until they're passed to reverse_invoker, which now copies
them as lvalues.

Fixes #80126
---
 pstl/test/support/utils.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/pstl/test/support/utils.h b/pstl/test/support/utils.h
index ed6d48b9471ac..e35084eabb20b 100644
--- a/pstl/test/support/utils.h
+++ b/pstl/test/support/utils.h
@@ -1083,18 +1083,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...);
     }
 };
 



More information about the libcxx-commits mailing list