ericniebler/range-v3

Infinite loop when using ranges::view::cycle with an infinite range.

tremmelg opened this issue · 2 comments

Using an infinite range in the example below as an input to ranges::view::cycle and accessing the result at a random location doesn't emit any errors and results in an infinite loop at runtime. Tested using 2ff4cf2.

#include <iostream>

#include <range/v3/all.hpp>

int main() {
  auto view = ranges::view::iota(0)
            | ranges::view::cycle;

  std::cout << view[5] << "\n";
}

The bug is in cycle_view::cursor::advance: it assumes that the base range has finite size:

CONCEPT_REQUIRES(RandomAccessRange<Rng>())
void advance(difference_type_ n)
{
    auto const begin = ranges::begin(rng_->rng_);
    auto const end = this->get_end_(BoundedRange<Rng>(), meta::bool_<true>()); // <-- BUGBUG
    auto const d = end - begin;
    auto const off = ((it_ - begin) + n) % d;
    it_ = begin + (off < 0 ? off + d : off);
}

I suspect that distance_to and advance are not implementable for view::cycle due to ericniebler/stl2#477 (comment). Consequently, view::cycle could be at best a BidirectionalRange.