[libcxx-commits] [PATCH] D68952: Guard against possible overflow in span.subpan

Michael Schellenberger Costa via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Oct 14 11:58:56 PDT 2019


miscco added a comment.

As a side note we could also simplify the non templated subspan method

  constexpr span<element_type, dynamic_extent>
  _LIBCPP_INLINE_VISIBILITY
  subspan(index_type __offset, index_type __count = dynamic_extent) const noexcept
  {
      _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
      _LIBCPP_ASSERT(__count  <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)");
      if (__count == dynamic_extent)
          return {data() + __offset, size() - __offset};
      _LIBCPP_ASSERT(__offset <= size() - __count, "Offset + count out of range in span::subspan(offset, count)");
      return {data() + __offset, __count};
  }

To the equivalent

  constexpr span<element_type, dynamic_extent>
  _LIBCPP_INLINE_VISIBILITY
  subspan(index_type __offset, index_type __count = dynamic_extent) const noexcept
  {
      _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)");
      _LIBCPP_ASSERT(__count == dynamic_extent || __offset <= size() - __count, "Offset + count out of range in span::subspan(offset, count)");
      return {data() + __offset, __count == dynamic_extent ? size() - __offset : __count };
  }

If `__count == dynamic_extent` then the second assert is never tested. If `__count != dynamic_extent` then `__count  <= size()` follows from conjunction of `__offset <= size()` and `__offset <= size() - __count`

However, I wasn't too sure whether it should go into the same revision


Repository:
  rCXX libc++

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D68952/new/

https://reviews.llvm.org/D68952





More information about the libcxx-commits mailing list