From b34d6e1754bfcfbdd4ce922754b446a5ee684e66 Mon Sep 17 00:00:00 2001 From: Tristan Brindle Date: Mon, 27 Nov 2023 17:17:29 +0000 Subject: [PATCH 1/2] Fix out-of-bounds inc() in detail::advance If we passed a positive offset to the non-RA version of advance(), we were performing an inc() on the passed-in cursor without first checking whether it was at the end. So let's not do that. Fixes #132 --- include/flux/op/stride.hpp | 10 +++++----- test/test_drop.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/include/flux/op/stride.hpp b/include/flux/op/stride.hpp index 237839d8..638cf9ed 100644 --- a/include/flux/op/stride.hpp +++ b/include/flux/op/stride.hpp @@ -18,12 +18,12 @@ inline constexpr struct advance_fn { constexpr auto operator()(Seq& seq, cursor_t& cur, distance_t offset) const -> distance_t { if (offset > 0) { - while (--offset >= 0) { - if (flux::is_last(seq, flux::inc(seq, cur))) { - break; - } + distance_t counter = 0; + while (offset-- > 0 && !flux::is_last(seq, cur)) { + flux::inc(seq, cur); + ++counter; } - return offset; + return counter; } else if (offset < 0) { if constexpr (bidirectional_sequence) { auto const fst = flux::first(seq); diff --git a/test/test_drop.cpp b/test/test_drop.cpp index 7471ed77..9fb209e8 100644 --- a/test/test_drop.cpp +++ b/test/test_drop.cpp @@ -116,6 +116,37 @@ constexpr bool test_drop() { } static_assert(test_drop()); +constexpr bool issue_132a() +{ + auto result = flux::from(std::array{1, 2}) + .filter(flux::pred::even) + .drop(2) + .drop(1); + STATIC_CHECK(flux::is_empty(result)); + return true; +} +static_assert(issue_132a()); + +void issue_132b() +{ + using namespace flux; + + auto intersperse = [](auto r, auto e) -> auto { + return flux::map(std::move(r), [e](auto const& x) -> auto { + return std::vector{e, x}; + }).flatten().drop(1); + }; + + auto sfml_argument = [](std::string_view) -> std::string { return "abc"; }; + + auto sfml_argument_list = [&](std::span mf) -> std::string { + return "(" + intersperse(drop(mf, 1).map(sfml_argument), std::string(", ")).flatten().to() + ")"; + }; + + std::vector v {"point"}; + (void) sfml_argument_list(v); +} + } TEST_CASE("drop") @@ -131,4 +162,7 @@ TEST_CASE("drop") REQUIRE_THROWS_AS(flux::from_range(list).drop(-1000), flux::unrecoverable_error); } + + issue_132b(); + } \ No newline at end of file From 173aa9db428884993fe8be711083fccc1ae19441 Mon Sep 17 00:00:00 2001 From: Tristan Brindle Date: Mon, 27 Nov 2023 17:23:21 +0000 Subject: [PATCH 2/2] Add missing #include in test_drop.cpp ...and a few other #includes just to be on the safe side --- test/test_drop.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/test_drop.cpp b/test/test_drop.cpp index 9fb209e8..b15cd211 100644 --- a/test/test_drop.cpp +++ b/test/test_drop.cpp @@ -11,6 +11,10 @@ #include #include +#include +#include +#include +#include namespace {