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

James Y Knight via libc-commits libc-commits at lists.llvm.org
Tue Oct 24 16:10:42 PDT 2023


jyknight wrote:

I think we can check whether it's been overridden more easily using an asm alias like this (when libc++ is built under -fno-exceptions):
```
#include <cstdlib>
#include <new>

__attribute__((weak)) void* operator new(std::size_t size) {
  void* mem = malloc(size);
  if (!mem) abort();
  return mem;
}

// TODO: ifdef for MSVC mangling.
static void* original_operator_new(std::size_t size) __attribute__((alias(__USER_LABEL_PREFIX__ "_Znwm")));

__attribute__((weak)) void* operator new(std::size_t size, std::nothrow_t) noexcept {
  if (static_cast<void*(*)(std::size_t)>(&::operator new) != original_operator_new)
    abort();
  return malloc(size);
}
```


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


More information about the libc-commits mailing list