Title
enumerate_view may invoke UB for sized common non-forward underlying ranges
Status
wp
Section
[range.enumerate]
Submitter
Patrick Palka

Created on 2023-04-07.00:00:00 last changed 3 weeks ago

Messages

Date: 2024-04-02.10:29:12

Proposed resolution:

This wording is relative to N4944.

  1. Modify [range.enumerate.view], class template class enumerate_view synopsis, as indicated:

    […]
    constexpr auto end() requires (!simple-view<V>) {
      if constexpr (forward_range<V> && common_range<V> && sized_range<V>)
        return iterator<false>(ranges::end(base_), ranges::distance(base_));
      else
        return sentinel<false>(ranges::end(base_));
    }
    constexpr auto end() const requires range-with-movable-references<const V> {
      if constexpr (forward_range<const V> && common_range<const V> && sized_range<const V>)
        return iterator<true>(ranges::end(base_), ranges::distance(base_));
      else
        return sentinel<true>(ranges::end(base_));
    }
    […]
    
Date: 2024-04-02.10:29:12

[ Tokyo 2024-03-23; Status changed: Voting → WP. ]

Date: 2023-11-10.19:42:21

[ Kona 2023-11-10; move to Ready ]

Date: 2023-05-15.00:00:00

[ 2023-05-24; Reflector poll ]

Set priority to 3 after reflector poll.

Date: 2022-04-15.00:00:00

[ 2022-04-12; Patrick Palka provides wording ]

The proposed wording follows the suggestion provided by Tim Song, to simply make enumerate non-common for this case.

Date: 2023-04-07.00:00:00

For a sized common range, enumerate_view::end() is specified to call ranges::distance. But ranges::distance is not necessarily well-defined for a sized non-forward range after calling ranges::begin (according to [range.sized]).

So for a sized common non-forward underlying range, it seems calling enumerate_view::begin() followed by enumerate_view::end() may invoke UB and thus make enumerate_view potentially unusable for such ranges.

I suppose we might need to instead call and cache the result of ranges::distance from enumerate_view::begin() for such ranges.

History
Date User Action Args
2024-04-02 10:29:12adminsetmessages: + msg14031
2024-04-02 10:29:12adminsetstatus: voting -> wp
2024-03-18 09:32:04adminsetstatus: ready -> voting
2023-11-10 19:42:21adminsetmessages: + msg13832
2023-11-10 19:42:21adminsetstatus: new -> ready
2023-05-24 14:33:00adminsetmessages: + msg13569
2023-04-15 13:22:37adminsetmessages: + msg13521
2023-04-15 13:22:37adminsetmessages: + msg13520
2023-04-07 00:00:00admincreate