Skip to content

Commit

Permalink
Switch to catching unhandled exceptions on Windows Closes #324 (#325)
Browse files Browse the repository at this point in the history
* Switch to catching unhandled exceptions on Windows
  • Loading branch information
jkriegshauser authored and onqtam committed Feb 24, 2020
1 parent 503de03 commit 2f3fd5e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
16 changes: 8 additions & 8 deletions doctest/doctest.h
Original file line number Diff line number Diff line change
Expand Up @@ -3941,10 +3941,11 @@ namespace {

struct FatalConditionHandler
{
static LONG CALLBACK handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo) {
static LONG CALLBACK handleException(PEXCEPTION_POINTERS ExceptionInfo) {
for(size_t i = 0; i < DOCTEST_COUNTOF(signalDefs); ++i) {
if(ExceptionInfo->ExceptionRecord->ExceptionCode == signalDefs[i].id) {
reportFatal(signalDefs[i].name);
break;
}
}
// If its not an exception we care about, pass it along.
Expand All @@ -3957,19 +3958,18 @@ namespace {
// 32k seems enough for doctest to handle stack overflow,
// but the value was found experimentally, so there is no strong guarantee
guaranteeSize = 32 * 1024;
exceptionHandlerHandle = nullptr;
// Register as first handler in current chain
exceptionHandlerHandle = AddVectoredExceptionHandler(1, handleVectoredException);
// Register an unhandled exception filter
previousTop = SetUnhandledExceptionFilter(handleException);
// Pass in guarantee size to be filled
SetThreadStackGuarantee(&guaranteeSize);
}

static void reset() {
if(isSet) {
// Unregister handler and restore the old guarantee
RemoveVectoredExceptionHandler(exceptionHandlerHandle);
SetUnhandledExceptionFilter(previousTop);
SetThreadStackGuarantee(&guaranteeSize);
exceptionHandlerHandle = nullptr;
previousTop = nullptr;
isSet = false;
}
}
Expand All @@ -3979,12 +3979,12 @@ namespace {
private:
static bool isSet;
static ULONG guaranteeSize;
static PVOID exceptionHandlerHandle;
static LPTOP_LEVEL_EXCEPTION_FILTER previousTop;
};

bool FatalConditionHandler::isSet = false;
ULONG FatalConditionHandler::guaranteeSize = 0;
PVOID FatalConditionHandler::exceptionHandlerHandle = nullptr;
LPTOP_LEVEL_EXCEPTION_FILTER FatalConditionHandler::previousTop = nullptr;

#else // DOCTEST_PLATFORM_WINDOWS

Expand Down
16 changes: 8 additions & 8 deletions doctest/parts/doctest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1329,10 +1329,11 @@ namespace {

struct FatalConditionHandler
{
static LONG CALLBACK handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo) {
static LONG CALLBACK handleException(PEXCEPTION_POINTERS ExceptionInfo) {
for(size_t i = 0; i < DOCTEST_COUNTOF(signalDefs); ++i) {
if(ExceptionInfo->ExceptionRecord->ExceptionCode == signalDefs[i].id) {
reportFatal(signalDefs[i].name);
break;
}
}
// If its not an exception we care about, pass it along.
Expand All @@ -1345,19 +1346,18 @@ namespace {
// 32k seems enough for doctest to handle stack overflow,
// but the value was found experimentally, so there is no strong guarantee
guaranteeSize = 32 * 1024;
exceptionHandlerHandle = nullptr;
// Register as first handler in current chain
exceptionHandlerHandle = AddVectoredExceptionHandler(1, handleVectoredException);
// Register an unhandled exception filter
previousTop = SetUnhandledExceptionFilter(handleException);
// Pass in guarantee size to be filled
SetThreadStackGuarantee(&guaranteeSize);
}

static void reset() {
if(isSet) {
// Unregister handler and restore the old guarantee
RemoveVectoredExceptionHandler(exceptionHandlerHandle);
SetUnhandledExceptionFilter(previousTop);
SetThreadStackGuarantee(&guaranteeSize);
exceptionHandlerHandle = nullptr;
previousTop = nullptr;
isSet = false;
}
}
Expand All @@ -1367,12 +1367,12 @@ namespace {
private:
static bool isSet;
static ULONG guaranteeSize;
static PVOID exceptionHandlerHandle;
static LPTOP_LEVEL_EXCEPTION_FILTER previousTop;
};

bool FatalConditionHandler::isSet = false;
ULONG FatalConditionHandler::guaranteeSize = 0;
PVOID FatalConditionHandler::exceptionHandlerHandle = nullptr;
LPTOP_LEVEL_EXCEPTION_FILTER FatalConditionHandler::previousTop = nullptr;

#else // DOCTEST_PLATFORM_WINDOWS

Expand Down

0 comments on commit 2f3fd5e

Please sign in to comment.