Skip to content

[libc++] Fix noexcept behaviour in _impl functions #74330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions libcxxabi/src/stdlib_new_delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
// in this shared library, so that they can be overridden by programs
// that define non-weak copies of the functions.

static void* operator_new_impl(std::size_t size) noexcept {
_LIBCPP_WEAK
void* operator new(std::size_t size) _THROW_BAD_ALLOC {
if (size == 0)
size = 1;
void* p;
Expand All @@ -41,18 +42,12 @@ static void* operator_new_impl(std::size_t size) noexcept {
if (nh)
nh();
else
break;
}
return p;
}

_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();
throw std::bad_alloc();
#else
break;
#endif
}
return p;
}

Expand Down Expand Up @@ -107,7 +102,8 @@ void operator delete[](void* ptr, size_t) noexcept { ::operator delete[](ptr); }

#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)

static void* operator_new_aligned_impl(std::size_t size, std::align_val_t alignment) noexcept {
_LIBCPP_WEAK
void* operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
if (size == 0)
size = 1;
if (static_cast<size_t>(alignment) < sizeof(void*))
Expand All @@ -116,24 +112,22 @@ static void* operator_new_aligned_impl(std::size_t size, std::align_val_t alignm
// Try allocating memory. If allocation fails and there is a new_handler,
// call it to try free up memory, and try again until it succeeds, or until
// the new_handler decides to terminate.
//
// If allocation fails and there is no new_handler, we throw bad_alloc
// (or return nullptr if exceptions are disabled).
void* p;
while ((p = std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment), size)) == nullptr) {
std::new_handler nh = std::get_new_handler();
if (nh)
nh();
else
break;
}
return p;
}

_LIBCPP_WEAK
void* operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
void* p = operator_new_aligned_impl(size, alignment);
else {
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
if (p == nullptr)
throw std::bad_alloc();
throw std::bad_alloc();
# else
break;
# endif
}
}
return p;
}

Expand Down
33 changes: 33 additions & 0 deletions libcxxabi/test/test_memory_alloc.pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <new>
#include <cassert>
#include <limits>

int new_handler_called = 0;

void my_new_handler() {
++new_handler_called;
throw std::bad_alloc();
}

int main(int, char**) {
std::set_new_handler(my_new_handler);
try {
void* x = operator new[] (std::numeric_limits<std::size_t>::max());
(void)x;
assert(false);
}
catch (std::bad_alloc const&) {
assert(new_handler_called == 1);
} catch (...) {
assert(false);
}
return 0;
}