From 9ced3148ca18e8b057e7f2a7e773c701b95c8741 Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Wed, 30 Jul 2025 10:33:33 +0200 Subject: [PATCH] util: avoid calling UNREACHABLE(str) macro without arguments After commit 2dcd6bed6a9 ("util: enforce unreachable()'s argument being a literal string", 2023-04-17) the compiler effectively emit errors when an argument that is not a string literal is passed to UNREACHABLE(str), however the compiler still allows the macro to be called without arguments, which can be confusing. Implement the type check outside of the assert() call so that we have two types of errors: 1. The compiler will error out when the argument passed to the macro is not a string literal because the concatenation with an empty string will not be allowed. 2. The compiler will error out when no arguments are passed to the macro because the invocation of assert() will be invalid. This also has the nice side-effect of removing the extra empty string printed in the assert() messages; after the changes the messages will look like: Assertion `!"Invalid type"' failed. instead of: Assertion `!"" "Invalid type"' failed. Fixes: 2dcd6bed6a9 ("util: enforce unreachable()'s argument being a literal string", 2023-04-17) Reviewed-by: Erik Faye-Lund Part-of: --- src/util/macros.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/util/macros.h b/src/util/macros.h index af4c8c80b60..9403d337e49 100644 --- a/src/util/macros.h +++ b/src/util/macros.h @@ -122,17 +122,23 @@ #if defined(HAVE___BUILTIN_UNREACHABLE) || __has_builtin(__builtin_unreachable) #define UNREACHABLE(str) \ do { \ - assert(!"" str); \ + (void)"" str; /* str must be a string literal */ \ + assert(!str); \ __builtin_unreachable(); \ } while (0) #elif defined (_MSC_VER) #define UNREACHABLE(str) \ do { \ - assert(!"" str); \ + (void)"" str; /* str must be a string literal */ \ + assert(!str); \ __assume(0); \ } while (0) #else -#define UNREACHABLE(str) assert(!"" str) +#define UNREACHABLE(str) \ +do { \ + (void)"" str; /* str must be a string literal */ \ + assert(!str); \ +} while (0) #endif /**