Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix out-of-bounds inc() in detail::advance #134

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 38 additions & 0 deletions test/test_drop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

#include <array>
#include <list>
#include <span>
#include <string>
#include <string_view>
#include <vector>

namespace {

Expand Down Expand Up @@ -116,6 +120,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 +166,7 @@ TEST_CASE("drop")

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

issue_132b();

}