@@ -19,6 +19,12 @@ Last reviewed:
1919#include < cstdlib>
2020#include < unistd.h>
2121#endif
22+ #include < stdlib.h>
23+ #if defined(WIN32) || defined(WIN64)
24+ #include < string.h>
25+ #else
26+ #include < strings.h>
27+ #endif
2228#include < stdio.h>
2329#include < assert.h>
2430#if defined(WIN32) || defined(WIN64)
@@ -38,6 +44,46 @@ void APIENTRY DefWarnProc(const char * pszExp, const char * pszFile, int nLine,
3844void APIENTRY DefAssertProc (const char * pszExp, const char * pszFile, int nLine, HMODULE hmod);
3945bool GetShowAssertMessageBox ();
4046
47+ static bool TryGetEnvironmentVariableValue (const char * pszName, char ** ppszValue)
48+ {
49+ #if defined(WIN32) || defined(WIN64)
50+ size_t cchValue = 0 ;
51+ return _dupenv_s (ppszValue, &cchValue, pszName) == 0 && *ppszValue != NULL ;
52+ #else
53+ *ppszValue = getenv (pszName);
54+ return *ppszValue != NULL ;
55+ #endif
56+ }
57+
58+ static void FreeEnvironmentVariableValue (char * pszValue)
59+ {
60+ #if defined(WIN32) || defined(WIN64)
61+ free (pszValue);
62+ #else
63+ (void )pszValue;
64+ #endif
65+ }
66+
67+ static bool EqualsIgnoreCase (const char * pszLeft, const char * pszRight)
68+ {
69+ #if defined(WIN32) || defined(WIN64)
70+ return _stricmp (pszLeft, pszRight) == 0 ;
71+ #else
72+ return strcasecmp (pszLeft, pszRight) == 0 ;
73+ #endif
74+ }
75+
76+ static bool IsTestModeEnabled ()
77+ {
78+ char * pTestMode = NULL ;
79+ if (!TryGetEnvironmentVariableValue (" FW_TEST_MODE" , &pTestMode))
80+ return false ;
81+
82+ const bool fIsTestMode = strcmp (pTestMode, " 1" ) == 0 ;
83+ FreeEnvironmentVariableValue (pTestMode);
84+ return fIsTestMode ;
85+ }
86+
4187typedef void (__stdcall * _DBG_REPORT_HOOK)(int , char *);
4288typedef int (__stdcall * _DBG_DISPLAYMSGBOX_HOOK)(char *);
4389
@@ -291,22 +337,10 @@ extern "C" __declspec(dllexport) int APIENTRY DebugProcsExit(void)
291337----------------------------------------------------------------------------------------------*/
292338bool GetShowAssertMessageBox ()
293339{
294- // getenv is deprecated on Windows
295- #if defined(WIN32) || defined(WIN64)
296- #pragma warning(push)
297- #pragma warning(disable: 4996)
298-
299- // Windows doesn't know strcasecmp, it calls it stricmp instead...
300- #ifndef strcasecmp
301- #define strcasecmp stricmp
302- #endif
303- #endif // WIN32
304-
305340 // FW_TEST_MODE is an unconditional override set by test runners.
306341 // It takes priority over both the registry and AssertUiEnabled so that
307342 // developer machines with the registry key set never pop dialogs during tests.
308- const char * pTestMode = getenv (" FW_TEST_MODE" );
309- if (pTestMode && strcasecmp (pTestMode, " 1" ) == 0 )
343+ if (IsTestModeEnabled ())
310344 return false ;
311345
312346#if defined(WIN32) || defined(WIN64)
@@ -324,14 +358,16 @@ bool GetShowAssertMessageBox()
324358 return fShowAssertMessageBox ? true : false ; // otherwise we get a performance warning
325359 }
326360#endif // WIN32
327- const char * pEnvVar = getenv (" AssertUiEnabled" );
328- return !pEnvVar ||
329- strcasecmp (pEnvVar, " 0" ) != 0 &&
330- strcasecmp (pEnvVar, " false" ) != 0 &&
331- strcasecmp (pEnvVar, " no" ) != 0 ;
332- #if defined(WIN32) || defined(WIN64)
333- #pragma warning(pop)
334- #endif // WIN32
361+ char * pEnvVar = NULL ;
362+ if (!TryGetEnvironmentVariableValue (" AssertUiEnabled" , &pEnvVar))
363+ return true ;
364+
365+ const bool fShowAssertMessageBox =
366+ !EqualsIgnoreCase (pEnvVar, " 0" ) &&
367+ !EqualsIgnoreCase (pEnvVar, " false" ) &&
368+ !EqualsIgnoreCase (pEnvVar, " no" );
369+ FreeEnvironmentVariableValue (pEnvVar);
370+ return fShowAssertMessageBox ;
335371}
336372
337373/* ----------------------------------------------------------------------------------------------
@@ -591,10 +627,13 @@ void __cdecl SilAssert (
591627 else
592628 OutputDebugString (assertbuf);
593629
594- // Always mirror assertion text to the process error stream so test runners,
595- // humans, and automation can see the failure details without a debugger.
596- fprintf (stderr, " %s\n " , assertbuf);
597- fflush (stderr);
630+ if (IsTestModeEnabled ())
631+ {
632+ // In test mode, mirror assertion text to stderr so unattended runs capture
633+ // the failure details without depending on a debugger or modal UI.
634+ fprintf (stderr, " %s\n " , assertbuf);
635+ fflush (stderr);
636+ }
598637
599638 // NOTE: this method is intented to be used by unmanaged apps only;
600639 // managed apps should use DebugProcs.AssertProc in DebugProcs.cs
0 commit comments