diff --git a/libcxxabi/src/cxa_default_handlers.cpp b/libcxxabi/src/cxa_default_handlers.cpp --- a/libcxxabi/src/cxa_default_handlers.cpp +++ b/libcxxabi/src/cxa_default_handlers.cpp @@ -22,6 +22,22 @@ static constinit const char* cause = "uncaught"; +template +_LIBCXXABI_HIDDEN +char const* demangle(char const* str, char (&buf)[N]) { +#if !defined(LIBCXXABI_NON_DEMANGLING_TERMINATE) + int status; + size_t len = sizeof(buf); + const char* result = __cxxabiv1::__cxa_demangle(str, buf, &len, &status); + if (status != 0) + return str; + else + return result; +#else + return str; +#endif +} + __attribute__((noreturn)) static void demangling_terminate_handler() { @@ -45,17 +61,8 @@ exception_header + 1; const __shim_type_info* thrown_type = static_cast(exception_header->exceptionType); -#if !defined(LIBCXXABI_NON_DEMANGLING_TERMINATE) - // Try to get demangled name of thrown_type - int status; char buf[1024]; - size_t len = sizeof(buf); - const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status); - if (status != 0) - name = thrown_type->name(); -#else - const char* name = thrown_type->name(); -#endif + char const* name = demangle(thrown_type->name(), buf); // If the uncaught exception can be caught with std::exception& const __shim_type_info* catch_type = static_cast(&typeid(std::exception));