[libcxx-commits] [PATCH] D88864: Add assert in `constexpr string_view::operator[]`

Chris Palmer via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Oct 5 17:17:08 PDT 2020


palmer created this revision.
palmer added reviewers: jdoerrie, EricWF.
palmer requested review of this revision.

Bounds checking.


https://reviews.llvm.org/D88864

Files:
  libcxx/include/string_view


Index: libcxx/include/string_view
===================================================================
--- libcxx/include/string_view
+++ libcxx/include/string_view
@@ -278,7 +278,9 @@
 
     // [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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D88864.296333.patch
Type: text/x-patch
Size: 641 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20201006/405d7340/attachment.bin>


More information about the libcxx-commits mailing list