Skip to content

Commit

Permalink
Implement a safer version of GetCrashInfoFromException (#3652)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update src/stacktraces.cpp

Co-authored-by: dustinface <[email protected]>

Co-authored-by: dustinface <[email protected]>
  • Loading branch information
2 people authored and charlesrocket committed Aug 15, 2020
1 parent 5d9195c commit 40e0f5d
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/stacktraces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<unknown>";
};

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 = "<unknown>";
}

if (type.empty()) {
type = "<unknown>";
} else {
type = DemangleSymbol(type);
}

ci.crashDescription += strprintf("type=%s, what=\"%s\"", type, what);

auto stackframes = GetExceptionStacktrace(e);
Expand Down

0 comments on commit 40e0f5d

Please sign in to comment.