23
23
#include < atomic>
24
24
#include < array>
25
25
#include < algorithm>
26
+ #include < cctype>
26
27
#include < mutex>
27
28
#include < signal.h>
28
29
#include < intrin.h> // For _ReturnAddress()
@@ -507,6 +508,20 @@ static void InstallVectoredHandler() noexcept
507
508
}
508
509
}
509
510
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
+
510
525
static void InstallSehHandler () noexcept
511
526
{
512
527
std::lock_guard<std::mutex> lock (g_handlerStateMutex);
@@ -523,6 +538,23 @@ static void InstallSehHandler() noexcept
523
538
std::array<char , DEBUG_BUFFER_SIZE> szDebug{};
524
539
SAFE_DEBUG_PRINT (szDebug, " %sSEH installed, previous filter at 0x%p\n " , DEBUG_PREFIX_CRASH, static_cast <const void *>(previousFilter));
525
540
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
+
526
558
if (g_pfnKernelSetUnhandledExceptionFilter.load (std::memory_order_acquire) != nullptr )
527
559
{
528
560
SafeDebugOutput (" CrashHandler: SEH detour protection already installed\n " );
0 commit comments