Skip to content

Commit

Permalink
Deregister SEH handler before reporting SE.
Browse files Browse the repository at this point in the history
  • Loading branch information
horenmar committed Feb 6, 2017
1 parent 4feb2db commit 2360060
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions include/internal/catch_fatal_condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace Catch {
static LONG CALLBACK handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo) {
for (int i = 0; i < sizeof(signalDefs) / sizeof(SignalDefs); ++i) {
if (ExceptionInfo->ExceptionRecord->ExceptionCode == signalDefs[i].id) {
reset();
reportFatal(signalDefs[i].name);
}
}
Expand All @@ -61,34 +62,41 @@ namespace Catch {
return EXCEPTION_CONTINUE_SEARCH;
}

// 32k seems enough for Catch to handle stack overflow,
// but the value was found experimentally, so there is no strong guarantee
FatalConditionHandler():m_isSet(true), m_guaranteeSize(32 * 1024), m_exceptionHandlerHandle(CATCH_NULL) {
FatalConditionHandler() {
isSet = true;
// 32k seems enough for Catch to handle stack overflow,
// but the value was found experimentally, so there is no strong guarantee
guaranteeSize = 32 * 1024;
exceptionHandlerHandle = CATCH_NULL;
// Register as first handler in current chain
m_exceptionHandlerHandle = AddVectoredExceptionHandler(1, handleVectoredException);
exceptionHandlerHandle = AddVectoredExceptionHandler(1, handleVectoredException);
// Pass in guarantee size to be filled
SetThreadStackGuarantee(&m_guaranteeSize);
SetThreadStackGuarantee(&guaranteeSize);
}

void reset() {
if (m_isSet) {
static void reset() {
if (isSet) {
// Unregister handler and restore the old guarantee
RemoveVectoredExceptionHandler(m_exceptionHandlerHandle);
SetThreadStackGuarantee(&m_guaranteeSize);
m_exceptionHandlerHandle = CATCH_NULL;
m_isSet = false;
RemoveVectoredExceptionHandler(exceptionHandlerHandle);
SetThreadStackGuarantee(&guaranteeSize);
exceptionHandlerHandle = CATCH_NULL;
isSet = false;
}
}

~FatalConditionHandler() {
reset();
}
private:
bool m_isSet;
ULONG m_guaranteeSize;
PVOID m_exceptionHandlerHandle;
static bool isSet;
static ULONG guaranteeSize;
static PVOID exceptionHandlerHandle;
};

bool FatalConditionHandler::isSet = false;
ULONG FatalConditionHandler::guaranteeSize = 0;
PVOID FatalConditionHandler::exceptionHandlerHandle = CATCH_NULL;

} // namespace Catch

# endif // CATCH_CONFIG_WINDOWS_SEH
Expand Down

0 comments on commit 2360060

Please sign in to comment.