Skip to content

[libc++abi] Remove unnecessary dependency on std::unique_ptr #73277

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

Merged
merged 1 commit into from
Aug 19, 2024
Merged
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
30 changes: 14 additions & 16 deletions libcxxabi/src/cxa_default_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
//===----------------------------------------------------------------------===//

#include <exception>
#include <memory>
#include <stdlib.h>
#include "abort_message.h"
#include "cxxabi.h"
#include "cxa_handlers.h"
Expand All @@ -23,17 +21,7 @@

static constinit const char* cause = "uncaught";

#ifndef _LIBCXXABI_NO_EXCEPTIONS
// Demangle the given string, or return the string as-is in case of an error.
static std::unique_ptr<char const, void (*)(char const*)> demangle(char const* str)
{
#if !defined(LIBCXXABI_NON_DEMANGLING_TERMINATE)
if (const char* result = __cxxabiv1::__cxa_demangle(str, nullptr, nullptr, nullptr))
return {result, [](char const* p) { std::free(const_cast<char*>(p)); }};
#endif
return {str, [](char const*) { /* nothing to free */ }};
}

# ifndef _LIBCXXABI_NO_EXCEPTIONS
__attribute__((noreturn))
static void demangling_terminate_handler()
{
Expand Down Expand Up @@ -61,20 +49,30 @@ static void demangling_terminate_handler()
exception_header + 1;
const __shim_type_info* thrown_type =
static_cast<const __shim_type_info*>(exception_header->exceptionType);
auto name = demangle(thrown_type->name());

auto name = [str = thrown_type->name()] {
# ifndef LIBCXXABI_NON_DEMANGLING_TERMINATE
if (const char* result = __cxxabiv1::__cxa_demangle(str, nullptr, nullptr, nullptr))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://reviews.llvm.org/D125268 is relevant for a discussion about whether it's OK to leak here given that we're terminating. I think the answer was "yes, it's fine". Tagging @urnathan for awareness.

I wonder if this could cause problems with sanitizers, though.

// We're about to abort(), this memory can never be freed; so it's fine
// to just return a raw pointer
return result;
# endif
return str;
}();

// If the uncaught exception can be caught with std::exception&
const __shim_type_info* catch_type =
static_cast<const __shim_type_info*>(&typeid(std::exception));
if (catch_type->can_catch(thrown_type, thrown_object))
{
// Include the what() message from the exception
const std::exception* e = static_cast<const std::exception*>(thrown_object);
abort_message("terminating due to %s exception of type %s: %s", cause, name.get(), e->what());
abort_message("terminating due to %s exception of type %s: %s", cause, name, e->what());
}
else
{
// Else just note that we're terminating due to an exception
abort_message("terminating due to %s exception of type %s", cause, name.get());
abort_message("terminating due to %s exception of type %s", cause, name);
}
}
#else // !_LIBCXXABI_NO_EXCEPTIONS
Expand Down
Loading