Fix dangling-else bugs in FAIL_FAST_IF and EMIT_USER_WARNING macros (#41504)
* Fix dangling-else bugs in FAIL_FAST_IF and EMIT_USER_WARNING macros Both macros were bare if-statements without do/while(0) guards, causing the dangling else problem when used as a single statement under an if. * Fix dangling-else bugs in FAIL_FAST_IF and EMIT_USER_WARNING macros * harden EMIT_USER_WARNING macro on Windows against dangling-else * Fix dangling-else bugs in FAIL_FAST_IF and EMIT_USER_WARNING macros
Eamon committed
Sep 4, 2026 at 01:13 UTC
dbb53be45a58bab2ce419e4178be72d49ce039c3
1 file changed
+12
-6
src/linux/inc/lxwil.h
+12
-6
@@ -16,10 +16,13 @@ namespace wil {
16
#define FAIL_FAST() raise(SIGABRT);
17
#define FAIL_FAST_CAUGHT_EXCEPTION() FAIL_FAST()
18
#define FAIL_FAST_IF(condition) \
19
- if ((condition)) \
19
+ do \
20
{ \
21
- FAIL_FAST() \
22
- }
21
+ if ((condition)) \
22
+ { \
23
+ FAIL_FAST(); \
24
+ } \
25
+ } while ((void)0, 0)
26
27
typedef void LogFunction(const char* message, const char* exceptionDescription) noexcept;
28
__declspec(selectany) LogFunction* g_LogExceptionCallback{};
@@ -650,10 +653,13 @@ using integral_from_enum = typename details::variable_size_mapping<T>::type;
653
#define WI_ASSERT(condition) assert(condition)
654
655
#define EMIT_USER_WARNING(Warning) \
653
- if (::wil::ScopedWarningsCollector::CanCollectWarning()) \
656
+ do \
657
{ \
655
- ::wil::ScopedWarningsCollector::CollectWarning(Warning); \
656
- }
658
+ if (::wil::ScopedWarningsCollector::CanCollectWarning()) \
659
+ { \
660
+ ::wil::ScopedWarningsCollector::CollectWarning(Warning); \
661
+ } \
662
+ } while ((void)0, 0)
663
664
class ScopedWarningsCollector
665
{