[libcxx-commits] [PATCH] D146379: [libc++] These throwing allocation functions shouldn't return null.

Betzalel Ganot via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue May 2 04:12:27 PDT 2023


BetzalelG added a comment.

In my opinion, it would be best to define a function here that will throw bad_alloc() under NO_EXCEPTIONS and otherwise abort. This is the same as throw_bad_array_new_length from cxa_vector.cpp.

  namespace {
  _LIBCXXABI_NORETURN
  void throw_bad_alloc() {
  #ifndef _LIBCXXABI_NO_EXCEPTIONS
      throw std::bad_alloc();
  #else
      abort_message("operator new failed to allocate memory");
  #endif
  }

And later use:

  if (nh)
      nh();
  else
    throw_bad_alloc();

nothrow function doesn't have to be changed in any way (except possibly to remove unnecessary `#ifndef _LIBCXXABI_NO_EXCEPTIONS` and `#endif` lines). Just call new operator, and we will either not propagate the exception, or abort.

New with nothrow is allowed to abort! This is similar to the behavior in libstdc++ and in Microsoft's Visual Studio implementation. I think this is the best behavior because it retains the convention you quoted of calling version (1).

Either way, libcxx new.cpp should similarly be fixed to have this behavior.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D146379



More information about the libcxx-commits mailing list