Skip to content

Commit

Permalink
Fix out-of-bounds inc() in detail::advance
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tcbrindle committed Nov 27, 2023
1 parent bc97bee commit b34d6e1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
10 changes: 5 additions & 5 deletions include/flux/op/stride.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ inline constexpr struct advance_fn {
constexpr auto operator()(Seq& seq, cursor_t<Seq>& 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<Seq>) {
auto const fst = flux::first(seq);
Expand Down
34 changes: 34 additions & 0 deletions test/test_drop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string_view> mf) -> std::string {
return "(" + intersperse(drop(mf, 1).map(sfml_argument), std::string(", ")).flatten().to<std::string>() + ")";
};

std::vector<std::string_view> v {"point"};
(void) sfml_argument_list(v);
}

}

TEST_CASE("drop")
Expand All @@ -131,4 +162,7 @@ TEST_CASE("drop")

REQUIRE_THROWS_AS(flux::from_range(list).drop(-1000), flux::unrecoverable_error);
}

issue_132b();

}

0 comments on commit b34d6e1

Please sign in to comment.