[libcxx-commits] [libcxxabi] [libc++] Fix the behavior of throwing `operator new` under -fno-exceptions (PR #69498)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 18 12:11:05 PDT 2023


================
@@ -38,39 +44,53 @@ static void* operator_new_impl(std::size_t size) noexcept {
 
 _LIBCPP_WEAK void* operator new(std::size_t size) _THROW_BAD_ALLOC {
   void* p = operator_new_impl(size);
-#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
   if (p == nullptr)
-    throw std::bad_alloc();
-#  endif
+    __throw_bad_alloc_shim();
   return p;
 }
 
 _LIBCPP_WEAK void* operator new(size_t size, const std::nothrow_t&) noexcept {
+#  ifdef _LIBCPP_HAS_NO_EXCEPTIONS
+  _LIBCPP_ASSERT_SHIM(
----------------
ldionne wrote:

@var-const This is a rare example of a case where I think it makes sense to unconditionally have a `_LIBCPP_ASSERT` -- this should be enabled regardless of the hardening mode IMO. It is similar to when we call `std::__throw_logic_error()` and friends with `-fno-exceptions` -- that aborts the program regardless of the hardening mode.

https://github.com/llvm/llvm-project/pull/69498


More information about the libcxx-commits mailing list