From 6a36073a00ac3a1fd23be79ad720a963f86bf9e3 Mon Sep 17 00:00:00 2001 From: Tom Jackson Date: Fri, 21 Aug 2015 10:28:33 -0700 Subject: [PATCH 1/1] Prevent accidental moves in filter() Summary: Per Boris's report. Preparing to re-push this after fixing downstream errors. Reviewed By: @ot Differential Revision: D2361333 --- folly/gen/Base-inl.h | 6 ++++-- folly/gen/test/BaseTest.cpp | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/folly/gen/Base-inl.h b/folly/gen/Base-inl.h index 0c4e92f4..23b91973 100644 --- a/folly/gen/Base-inl.h +++ b/folly/gen/Base-inl.h @@ -559,7 +559,8 @@ class Filter : public Operator> { template void foreach(Body&& body) const { source_.foreach([&](Value value) { - if (pred_(std::forward(value))) { + // NB: Argument not forwarded to avoid accidental move-construction + if (pred_(value)) { body(std::forward(value)); } }); @@ -568,7 +569,8 @@ class Filter : public Operator> { template bool apply(Handler&& handler) const { return source_.apply([&](Value value) -> bool { - if (pred_(std::forward(value))) { + // NB: Argument not forwarded to avoid accidental move-construction + if (pred_(value)) { return handler(std::forward(value)); } return true; diff --git a/folly/gen/test/BaseTest.cpp b/folly/gen/test/BaseTest.cpp index 7741b053..e97cf6c3 100644 --- a/folly/gen/test/BaseTest.cpp +++ b/folly/gen/test/BaseTest.cpp @@ -289,6 +289,15 @@ TEST(Gen, FilterDefault) { } } +TEST(Gen, FilterSink) { + auto actual + = seq(1, 2) + | map([](int x) { return vector{x}; }) + | filter([](vector v) { return !v.empty(); }) + | as(); + EXPECT_FALSE(from(actual) | rconcat | isEmpty); +} + TEST(Gen, Contains) { { auto gen = -- 2.34.1