[Bug libstdc++/107572] New: cartesian_product_view invokes the begin of input_range twice

hewillk at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Nov 8 15:25:46 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107572

            Bug ID: 107572
           Summary: cartesian_product_view invokes the begin of
                    input_range twice
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hewillk at gmail dot com
  Target Milestone: ---

ranges#L8069:

      auto __it = __detail::__tuple_transform(ranges::begin, _M_bases);
      if (!__empty_tail)
        std::get<0>(__it) =
__detail::__cartesian_common_arg_end(std::get<0>(_M_bases));
      return _Iterator<false>{*this, std::move(__it)};

This *always* invokes begin twice for the first range, which is undefined
behavior when it is an input_range.

testcase (https://godbolt.org/z/7hfP8xM6a):

    #include <ranges>
    #include <fmt/ranges.h>
    #include <sstream>

    std::istringstream ints("0 1 2 3 4");
    struct istream_range {
      auto begin() { return std::istream_iterator<int>{ints}; }
      auto end()   { return std::istream_iterator<int>{}; }
    };

    int main() {
      istream_range r;
      fmt::print("{}\n", std::views::cartesian_product(r)); // prints [(0),
(2), (3), (4)]
    }


More information about the Gcc-bugs mailing list