Skip to content

Commit 687c589

Browse files
Abseil Teamcopybara-github
authored andcommitted
Print stack traces on SEH exceptions on Windows
Also tidies up a couple of things: - Prevent handling of stack overflows, which cannot be done safely - `exception_code` is a macro, so we rename it - The `std::string` heap allocation was unnecessary Fixes #4298 PiperOrigin-RevId: 544117790 Change-Id: I8ba61f87119d5fbdb1f653700d9867ca6f8c28ce
1 parent 2acd538 commit 687c589

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

googletest/src/gtest.cc

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ bool UnitTestOptions::FilterMatchesTest(const std::string& test_suite_name,
866866
// Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the
867867
// given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise.
868868
// This function is useful as an __except condition.
869-
int UnitTestOptions::GTestShouldProcessSEH(DWORD exception_code) {
869+
int UnitTestOptions::GTestShouldProcessSEH(DWORD seh_code) {
870870
// Google Test should handle a SEH exception if:
871871
// 1. the user wants it to, AND
872872
// 2. this is not a breakpoint exception, AND
@@ -881,9 +881,11 @@ int UnitTestOptions::GTestShouldProcessSEH(DWORD exception_code) {
881881

882882
if (!GTEST_FLAG_GET(catch_exceptions))
883883
should_handle = false;
884-
else if (exception_code == EXCEPTION_BREAKPOINT)
884+
else if (seh_code == EXCEPTION_BREAKPOINT)
885885
should_handle = false;
886-
else if (exception_code == kCxxExceptionCode)
886+
else if (seh_code == EXCEPTION_STACK_OVERFLOW)
887+
should_handle = false;
888+
else if (seh_code == kCxxExceptionCode)
887889
should_handle = false;
888890

889891
return should_handle ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
@@ -2555,17 +2557,12 @@ bool Test::HasSameFixtureClass() {
25552557

25562558
#if GTEST_HAS_SEH
25572559

2558-
// Adds an "exception thrown" fatal failure to the current test. This
2559-
// function returns its result via an output parameter pointer because VC++
2560-
// prohibits creation of objects with destructors on stack in functions
2561-
// using __try (see error C2712).
2562-
static std::string* FormatSehExceptionMessage(DWORD exception_code,
2563-
const char* location) {
2560+
static std::string FormatSehExceptionMessage(DWORD exception_code,
2561+
const char* location) {
25642562
Message message;
25652563
message << "SEH exception with code 0x" << std::setbase(16) << exception_code
25662564
<< std::setbase(10) << " thrown in " << location << ".";
2567-
2568-
return new std::string(message.GetString());
2565+
return message.GetString();
25692566
}
25702567

25712568
#endif // GTEST_HAS_SEH
@@ -2613,14 +2610,20 @@ Result HandleSehExceptionsInMethodIfSupported(T* object, Result (T::*method)(),
26132610
return (object->*method)();
26142611
} __except (internal::UnitTestOptions::GTestShouldProcessSEH( // NOLINT
26152612
GetExceptionCode())) {
2616-
// We create the exception message on the heap because VC++ prohibits
2617-
// creation of objects with destructors on stack in functions using __try
2613+
// We wrap an inner function because VC++ prohibits direct creation of
2614+
// objects with destructors on stack in functions using __try
26182615
// (see error C2712).
2619-
std::string* exception_message =
2620-
FormatSehExceptionMessage(GetExceptionCode(), location);
2621-
internal::ReportFailureInUnknownLocation(TestPartResult::kFatalFailure,
2622-
*exception_message);
2623-
delete exception_message;
2616+
struct Wrapper {
2617+
static void ReportFailure(DWORD code, const char* location) {
2618+
return internal::ReportFailureInUnknownLocation(
2619+
TestPartResult::kFatalFailure,
2620+
FormatSehExceptionMessage(code, location) +
2621+
"\n"
2622+
"Stack trace:\n" +
2623+
::testing::internal::GetCurrentOsStackTraceExceptTop(1));
2624+
}
2625+
};
2626+
Wrapper::ReportFailure(GetExceptionCode(), location);
26242627
return static_cast<Result>(0);
26252628
}
26262629
#else

0 commit comments

Comments
 (0)