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

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Oct 6 13:58:08 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG9eff07a746a9: [libc++] Add assert to check bounds in `constexpr string_view::operator[]` (authored by palmer, committed by ldionne).
Herald added a project: libc++.
Herald added a reviewer: libc++.

Repository:
  rG LLVM Github Monorepo

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

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.296541.patch
Type: text/x-patch
Size: 641 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20201006/5cb3e5f9/attachment.bin>


More information about the libcxx-commits mailing list