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

Chris Palmer via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri May 8 12:20:30 PDT 2020


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

Going back to the return-expression form.


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,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
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.262928.patch
Type: text/x-patch
Size: 1140 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200508/a6f1dc04/attachment.bin>


More information about the libcxx-commits mailing list