Issue 234166
Summary [libc++] Ensure operations on random access iterators are not performed with operand of an invalid type
Labels libc++, rejects-valid
Assignees
Reporter frederick-vs-ja
    Let `i` be a random access iterator (either _Cpp17RandomAccessIterator_ or `random_access_iterator`), `n` be some difference value. The standard only requires related operations (namely `i[n]`, `i + n`, `n + i`, `i - n`, `i += n`, and `i -= n`) to be well-defined when the type of `n` is the difference type of the type of `i`.

So the following program should be well-formed, but currently libc++ rejects it. [Godbolt link](https://godbolt.org/z/hGzTasE6s).

```C++
#include <cstddef>
#include <functional>
#include <iterator>

class pedantic_rait {
private:
  int* it_ = nullptr;

public:
  using iterator_category = std::random_access_iterator_tag;
  using iterator_concept  = std::contiguous_iterator_tag;
  using reference         = int&;
  using pointer           = int*;
  using difference_type   = std::ptrdiff_t;
 using value_type        = int;

  pedantic_rait() = default;
  constexpr explicit pedantic_rait(int* p) : it_{p} {}

  constexpr reference operator*() const { return *it_; }
  constexpr pointer operator->() const { return it_; }
  constexpr reference operator[](std::same_as<difference_type> auto n) const { return it_[n]; }

  constexpr pedantic_rait& operator++() {
    ++it_;
    return *this;
  }
  constexpr pedantic_rait& operator--() {
    --it_;
    return *this;
  }
  constexpr pedantic_rait operator++(int) {
    auto old = *this;
    ++*this;
    return old;
 }
  constexpr pedantic_rait operator--(int) {
    auto old = *this;
 --*this;
    return old;
  }

  constexpr pedantic_rait& operator+=(std::same_as<difference_type> auto n) {
    it_ += n;
    return *this;
  }
  constexpr pedantic_rait& operator-=(std::same_as<difference_type> auto n) {
    it_ -= n;
    return *this;
  }
  friend constexpr pedantic_rait operator+(pedantic_rait x, std::same_as<difference_type> auto n) {
    x += n;
    return x;
  }
 friend constexpr pedantic_rait operator+(std::same_as<difference_type> auto n, pedantic_rait x) {
    x += n;
    return x;
  }
  friend constexpr pedantic_rait operator-(pedantic_rait x, std::same_as<difference_type> auto n) {
    x -= n;
    return x;
  }
  friend constexpr difference_type operator-(pedantic_rait x, pedantic_rait y) { return x.it_ - y.it_; }

 friend bool operator==(const pedantic_rait&, const pedantic_rait&)  = default;
  friend auto operator<=>(const pedantic_rait&, const pedantic_rait&) = default;

  void operator,(const auto&) = delete;
  friend void operator,(const auto&, const pedantic_rait&) = delete;
};

static_assert(std::contiguous_iterator<pedantic_rait>);

int main() {
  using bms = std::boyer_moore_searcher<pedantic_rait>;

  int a[1]{};
  (void)bms{pedantic_rait{a + 0}, pedantic_rait{a + 1}};
}
```

In the whole libc++, we should cast the operand to the difference type if the iterator is potentially user-provided.

There're some related standard issues.
- [P2393R1](https://wg21.link/p2329r1) #105171
- [LWG3717](https://cplusplus.github.io/LWG/issue3717) #105008
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to