[libcxx-commits] [libcxxabi] [libcxx] [libc++] Fix noexcept behaviour of operator new helper functions (PR #74337)
Hubert Tong via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Dec 6 16:23:21 PST 2023
================
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: no-exceptions
+
+#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&) {
----------------
hubert-reinterpretcast wrote:
I believe it would be prudent to throw (and catch), within the test, an exception of a class type (defined as part of the test itself) derived from `std::bad_alloc`. If extra paranoid, the class type should also record the value of the `this` pointer on construction using a "tag" class type and have a copy constructor that `abort()`s:
```cpp
struct my_bad_alloc : std::bad_alloc {
my_bad_alloc(const my_bad_alloc &) { abort(); }
my_bad_alloc(construction_key) self(this) {}
const my_bad_alloc *const self;
};
```
The recorded pointer value can then be checked against the address of the caught exception object in the catch block.
https://github.com/llvm/llvm-project/pull/74337
More information about the libcxx-commits
mailing list