From bd5c047e28d3e313ac0e1ae1cb47f7ead5f1e6cd Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Fri, 14 Aug 2020 14:42:42 +0300 Subject: [PATCH] Implement a safer version of GetCrashInfoFromException (#3652) * Implement a safer version of GetCrashInfoFromException `abi::__cxa_current_exception_type()` can return `null`, handle this properly * Update src/stacktraces.cpp Co-authored-by: dustinface <35775977+xdustinface@users.noreply.github.com> * Update src/stacktraces.cpp Co-authored-by: dustinface <35775977+xdustinface@users.noreply.github.com> Co-authored-by: dustinface <35775977+xdustinface@users.noreply.github.com> --- src/stacktraces.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/stacktraces.cpp b/src/stacktraces.cpp index dfe970ba60..020b7fee2b 100644 --- a/src/stacktraces.cpp +++ b/src/stacktraces.cpp @@ -690,32 +690,34 @@ crash_info GetCrashInfoFromException(const std::exception_ptr& e) std::string type; std::string what; + auto getExceptionType = [&]() -> std::string { + auto type = abi::__cxa_current_exception_type(); + if (type && (strlen(type->name()) > 0)) { + return DemangleSymbol(type->name()); + } + return ""; + }; + try { // rethrow and catch the exception as there is no other way to reliably cast to the real type (not possible with RTTI) std::rethrow_exception(e); } catch (const std::exception& e) { - type = abi::__cxa_current_exception_type()->name(); + type = getExceptionType(); what = GetExceptionWhat(e); } catch (const std::string& e) { - type = abi::__cxa_current_exception_type()->name(); + type = getExceptionType(); what = GetExceptionWhat(e); } catch (const char* e) { - type = abi::__cxa_current_exception_type()->name(); + type = getExceptionType(); what = GetExceptionWhat(e); } catch (int e) { - type = abi::__cxa_current_exception_type()->name(); + type = getExceptionType(); what = GetExceptionWhat(e); } catch (...) { - type = abi::__cxa_current_exception_type()->name(); + type = getExceptionType(); what = ""; } - if (type.empty()) { - type = ""; - } else { - type = DemangleSymbol(type); - } - ci.crashDescription += strprintf("type=%s, what=\"%s\"", type, what); auto stackframes = GetExceptionStacktrace(e);