Skip to content

Commit 4c967af

Browse files
[libc++abi] Remove unnecessary dependency on std::unique_ptr (#73277)
The demangling terminate handler uses a function `demangle()` to perform the demangling. This function returns an `std::unique_ptr`, relying on custom deleter and `const_cast` hacks to deal with the facts that only one branch actually allocates, and that the pointer type needs to be `const char*`. However, the destructor of the returned `std::unique_ptr` will never actually run, because the sole place this function is ever called is right before the terminate handler aborts the program. So all this is unnecessary, and creates a dependency onto `<memory>`. This change removes the `demangle()` function and replaces the call with an immediately invoked lambda expression that simply returns a raw pointer in both cases, which should be fine because the memory can never be freed here anyways.
1 parent 7597e09 commit 4c967af

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

libcxxabi/src/cxa_default_handlers.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
#include <exception>
13-
#include <memory>
14-
#include <stdlib.h>
1513
#include "abort_message.h"
1614
#include "cxxabi.h"
1715
#include "cxa_handlers.h"
@@ -23,17 +21,7 @@
2321

2422
static constinit const char* cause = "uncaught";
2523

26-
#ifndef _LIBCXXABI_NO_EXCEPTIONS
27-
// Demangle the given string, or return the string as-is in case of an error.
28-
static std::unique_ptr<char const, void (*)(char const*)> demangle(char const* str)
29-
{
30-
#if !defined(LIBCXXABI_NON_DEMANGLING_TERMINATE)
31-
if (const char* result = __cxxabiv1::__cxa_demangle(str, nullptr, nullptr, nullptr))
32-
return {result, [](char const* p) { std::free(const_cast<char*>(p)); }};
33-
#endif
34-
return {str, [](char const*) { /* nothing to free */ }};
35-
}
36-
24+
# ifndef _LIBCXXABI_NO_EXCEPTIONS
3725
__attribute__((noreturn))
3826
static void demangling_terminate_handler()
3927
{
@@ -61,20 +49,30 @@ static void demangling_terminate_handler()
6149
exception_header + 1;
6250
const __shim_type_info* thrown_type =
6351
static_cast<const __shim_type_info*>(exception_header->exceptionType);
64-
auto name = demangle(thrown_type->name());
52+
53+
auto name = [str = thrown_type->name()] {
54+
# ifndef LIBCXXABI_NON_DEMANGLING_TERMINATE
55+
if (const char* result = __cxxabiv1::__cxa_demangle(str, nullptr, nullptr, nullptr))
56+
// We're about to abort(), this memory can never be freed; so it's fine
57+
// to just return a raw pointer
58+
return result;
59+
# endif
60+
return str;
61+
}();
62+
6563
// If the uncaught exception can be caught with std::exception&
6664
const __shim_type_info* catch_type =
6765
static_cast<const __shim_type_info*>(&typeid(std::exception));
6866
if (catch_type->can_catch(thrown_type, thrown_object))
6967
{
7068
// Include the what() message from the exception
7169
const std::exception* e = static_cast<const std::exception*>(thrown_object);
72-
abort_message("terminating due to %s exception of type %s: %s", cause, name.get(), e->what());
70+
abort_message("terminating due to %s exception of type %s: %s", cause, name, e->what());
7371
}
7472
else
7573
{
7674
// Else just note that we're terminating due to an exception
77-
abort_message("terminating due to %s exception of type %s", cause, name.get());
75+
abort_message("terminating due to %s exception of type %s", cause, name);
7876
}
7977
}
8078
#else // !_LIBCXXABI_NO_EXCEPTIONS

0 commit comments

Comments
 (0)