Skip to content

Commit

Permalink
Fix read of dangling reference
Browse files Browse the repository at this point in the history
Fixes: #2289
  • Loading branch information
bernhardmgruber committed Aug 26, 2024
1 parent c1c1d96 commit 38cacdf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
23 changes: 10 additions & 13 deletions thrust/testing/functional_placeholders_miscellaneous.cu
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,14 @@ VectorUnitTest<TestFunctionalPlaceholdersTransformIterator,
VectorUnitTest<TestFunctionalPlaceholdersTransformIterator, ThirtyTwoBitTypes, thrust::host_vector, std::allocator>
TestFunctionalPlaceholdersTransformIteratorInstanceHost;

template <typename T>
struct TestFunctionalPlaceholdersArgumentValueCategories
void TestFunctionalPlaceholdersArgumentValueCategories()
{
void operator()() const
{
using namespace thrust::placeholders;
auto expr = _1 * _1 + _2 * _2;
T a = 2;
T b = 3;
ASSERT_ALMOST_EQUAL(expr(2, 3), 13); // pass pr-value
ASSERT_ALMOST_EQUAL(expr(a, b), 13); // pass l-value
ASSERT_ALMOST_EQUAL(expr(::cuda::std::move(a), ::cuda::std::move(b)), 13); // pass x-value
}
};
using namespace thrust::placeholders;
auto expr = _1 * _1 + _2 * _2;
int a = 2;
int b = 3;
ASSERT_EQUAL(expr(2, 3), 13); // pass pr-value
ASSERT_EQUAL(expr(a, b), 13); // pass l-value
ASSERT_EQUAL(expr(::cuda::std::move(a), ::cuda::std::move(b)), 13); // pass x-value
}
DECLARE_UNITTEST(TestFunctionalPlaceholdersArgumentValueCategories);
12 changes: 8 additions & 4 deletions thrust/thrust/detail/functional/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ struct argument
{
template <typename... Ts>
_CCCL_HOST_DEVICE auto
eval(Ts&&... args) const -> decltype(thrust::get<Pos>(thrust::tuple<Ts...>{THRUST_FWD(args)...}))
eval(Ts&&... args) const -> decltype(thrust::get<Pos>(thrust::tuple<Ts&&...>{THRUST_FWD(args)...}))
{
return thrust::get<Pos>(thrust::tuple<Ts...>{THRUST_FWD(args)...});
return thrust::get<Pos>(thrust::tuple<Ts&&...>{THRUST_FWD(args)...});
}
};

Expand All @@ -102,6 +102,8 @@ struct composite;
template <typename Eval, typename SubExpr>
struct composite<Eval, SubExpr>
{
constexpr composite() = default;

// TODO(bgruber): drop ctor and use aggregate initialization in C++17
_CCCL_HOST_DEVICE composite(const Eval& eval, const SubExpr& subexpr)
: m_eval(eval)
Expand All @@ -115,14 +117,15 @@ struct composite<Eval, SubExpr>
return m_eval.eval(m_subexpr.eval(THRUST_FWD(args)...));
}

private:
Eval m_eval;
SubExpr m_subexpr;
};

template <typename Eval, typename SubExpr1, typename SubExpr2>
struct composite<Eval, SubExpr1, SubExpr2>
{
constexpr composite() = default;

// TODO(bgruber): drop ctor and use aggregate initialization in C++17
_CCCL_HOST_DEVICE composite(const Eval& eval, const SubExpr1& subexpr1, const SubExpr2& subexpr2)
: m_eval(eval)
Expand All @@ -138,7 +141,6 @@ struct composite<Eval, SubExpr1, SubExpr2>
return m_eval.eval(m_subexpr1.eval(THRUST_FWD(args)...), m_subexpr2.eval(THRUST_FWD(args)...));
}

private:
Eval m_eval;
SubExpr1 m_subexpr1;
SubExpr2 m_subexpr2;
Expand All @@ -151,6 +153,8 @@ struct actor;
template <typename F>
struct operator_adaptor : F
{
constexpr operator_adaptor() = default;

_CCCL_HOST_DEVICE operator_adaptor(F f)
: F(::cuda::std::move(f))
{}
Expand Down

0 comments on commit 38cacdf

Please sign in to comment.