From 23600609c0b31195fff8c1db4f6821fb6f8dee74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Mon, 6 Feb 2017 20:40:46 +0100 Subject: [PATCH] Deregister SEH handler before reporting SE. --- include/internal/catch_fatal_condition.hpp | 36 +++++++++++++--------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/include/internal/catch_fatal_condition.hpp b/include/internal/catch_fatal_condition.hpp index bb5e850b..62da63e4 100644 --- a/include/internal/catch_fatal_condition.hpp +++ b/include/internal/catch_fatal_condition.hpp @@ -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); } } @@ -61,22 +62,25 @@ 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; } } @@ -84,11 +88,15 @@ namespace Catch { 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