2323#include < atomic>
2424#include < array>
2525#include < algorithm>
26+ #include < cctype>
2627#include < mutex>
2728#include < signal.h>
2829#include < intrin.h> // For _ReturnAddress()
@@ -507,6 +508,20 @@ static void InstallVectoredHandler() noexcept
507508 }
508509}
509510
511+ static bool TryReadEnvBool (const char * name, bool & outValue) noexcept
512+ {
513+ std::array<char , 8 > buffer{};
514+ DWORD length = GetEnvironmentVariableA (name, buffer.data (), static_cast <DWORD>(buffer.size ()));
515+
516+ if (length == 0 || length >= buffer.size ())
517+ return false ;
518+
519+ const unsigned char rawChar = static_cast <unsigned char >(buffer[0 ]);
520+ const char indicator = static_cast <char >(std::tolower (rawChar));
521+ outValue = !(indicator == ' 0' || indicator == ' f' || indicator == ' n' );
522+ return true ;
523+ }
524+
510525static void InstallSehHandler () noexcept
511526{
512527 std::lock_guard<std::mutex> lock (g_handlerStateMutex);
@@ -523,6 +538,23 @@ static void InstallSehHandler() noexcept
523538 std::array<char , DEBUG_BUFFER_SIZE> szDebug{};
524539 SAFE_DEBUG_PRINT (szDebug, " %sSEH installed, previous filter at 0x%p\n " , DEBUG_PREFIX_CRASH, static_cast <const void *>(previousFilter));
525540
541+ bool disableDetour = false ;
542+ bool forceDetour = false ;
543+ const bool hasDisableFlag = TryReadEnvBool (" MTA_DISABLE_SEH_DETOUR" , disableDetour);
544+ TryReadEnvBool (" MTA_FORCE_SEH_DETOUR" , forceDetour);
545+
546+ if (hasDisableFlag && disableDetour && !forceDetour)
547+ {
548+ SafeDebugOutput (" CrashHandler: SEH detour protection disabled by environment\n " );
549+ return ;
550+ }
551+
552+ if (!forceDetour && IsDebuggerPresent () == FALSE )
553+ {
554+ SafeDebugOutput (" CrashHandler: SEH detour protection skipped\n " );
555+ return ;
556+ }
557+
526558 if (g_pfnKernelSetUnhandledExceptionFilter.load (std::memory_order_acquire) != nullptr )
527559 {
528560 SafeDebugOutput (" CrashHandler: SEH detour protection already installed\n " );
0 commit comments