[libcxx-commits] [libcxx] 9eff07a - [libc++] Add assert to check bounds in `constexpr string_view::operator[]`

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Oct 6 13:57:55 PDT 2020


Author: Chris Palmer
Date: 2020-10-06T16:57:41-04:00
New Revision: 9eff07a746a9e6a9b105c12b5e28073360fa8065

URL: https://github.com/llvm/llvm-project/commit/9eff07a746a9e6a9b105c12b5e28073360fa8065
DIFF: https://github.com/llvm/llvm-project/commit/9eff07a746a9e6a9b105c12b5e28073360fa8065.diff

LOG: [libc++] Add assert to check bounds in `constexpr string_view::operator[]`

Differential Revision: https://reviews.llvm.org/D88864

Added: 
    

Modified: 
    libcxx/include/string_view

Removed: 
    


################################################################################
diff  --git a/libcxx/include/string_view b/libcxx/include/string_view
index 8a684a8f966c..884bcf806c45 100644
--- a/libcxx/include/string_view
+++ b/libcxx/include/string_view
@@ -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


        


More information about the libcxx-commits mailing list