[libc++] Add assert to check bounds in `constexpr string_view::operator[]`
authorChris Palmer <palmer@google.com>
Tue, 6 Oct 2020 17:01:50 +0000 (13:01 -0400)
committerLouis Dionne <ldionne@apple.com>
Tue, 6 Oct 2020 20:57:41 +0000 (16:57 -0400)
Differential Revision: https://reviews.llvm.org/D88864

libcxx/include/string_view

index 8a684a8..884bcf8 100644 (file)
@@ -278,7 +278,9 @@ public:
 
     // [string.view.access], element access
     _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
-    const_reference operator[](size_type __pos) const _NOEXCEPT { return __data[__pos]; }
+    const_reference operator[](size_type __pos) const _NOEXCEPT {
+      return _LIBCPP_ASSERT(__pos < size(), "string_view[] index out of bounds"), __data[__pos];
+    }
 
     _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
     const_reference at(size_type __pos) const