[libcxx-commits] [PATCH] D70343: Add a `_LIBCPP_HARDEN` define

Chris Palmer via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 26 14:21:27 PST 2019


palmer updated this revision to Diff 231135.
palmer added a comment.

Respond to comments.


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

https://reviews.llvm.org/D70343

Files:
  libcxx/include/__debug
  libcxx/include/string_view


Index: libcxx/include/string_view
===================================================================
--- libcxx/include/string_view
+++ libcxx/include/string_view
@@ -278,7 +278,10 @@
 
     // [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 {
+      _LIBCPP_ASSERT(__pos < size(), "string_view[] index out of bounds");
+      return __data[__pos];
+    }
 
     _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
     const_reference at(size_type __pos) const
Index: libcxx/include/__debug
===================================================================
--- libcxx/include/__debug
+++ libcxx/include/__debug
@@ -40,7 +40,11 @@
 #endif
 
 #ifndef _LIBCPP_ASSERT
-#   define _LIBCPP_ASSERT(x, m) ((void)0)
+#   ifdef _LIBCPP_HARDEN
+#      define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : __builtin_trap())
+#   else
+#      define _LIBCPP_ASSERT(x, m) ((void)0)
+#   endif
 #endif
 #ifndef _LIBCPP_DEBUG_ASSERT
 #   define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70343.231135.patch
Type: text/x-patch
Size: 1148 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20191126/ae4e6946/attachment.bin>


More information about the libcxx-commits mailing list