From e1ece63942a013c38881323bac3c405053a53090 Mon Sep 17 00:00:00 2001 From: Daniel Pouzzner Date: Tue, 8 Apr 2025 19:01:44 +0000 Subject: [PATCH] wolfssl/wolfcrypt/types.h: refactor fallback implementation of wc_static_assert() to depend on __COUNTER__, to avoid namespace collisions on unlucky macro invocations with same line numbers. --- .wolfssl_known_macro_extras | 1 + wolfssl/wolfcrypt/types.h | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.wolfssl_known_macro_extras b/.wolfssl_known_macro_extras index 0dc46fcab..aa797884c 100644 --- a/.wolfssl_known_macro_extras +++ b/.wolfssl_known_macro_extras @@ -887,6 +887,7 @@ __BIG_ENDIAN__ __BORLANDC__ __CCRX__ __COMPILER_VER__ +__COUNTER__ __CYGWIN__ __DATE__ __DCACHE_PRESENT diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index e773d9c46..a3e6af7aa 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -1806,8 +1806,8 @@ typedef struct w64wrapper { #define PRAGMA_DIAG_POP /* null expansion */ #endif - #define WC_CPP_CAT_(a, b) a ## b - #define WC_CPP_CAT(a, b) WC_CPP_CAT_(a, b) + #define WC_CPP_CAT4_(a, b, c, d) a ## b ## c ## d + #define WC_CPP_CAT4(a, b, c, d) WC_CPP_CAT4_(a, b, c, d) #if defined(WC_NO_STATIC_ASSERT) #define wc_static_assert(expr) struct wc_static_assert_dummy_struct #define wc_static_assert2(expr, msg) wc_static_assert(expr) @@ -1844,11 +1844,16 @@ typedef struct w64wrapper { #define wc_static_assert2(expr, msg) _Static_assert(expr, msg) #endif #else - /* C89-compatible fallback */ - #define wc_static_assert(expr) \ - struct WC_CPP_CAT(wc_static_assert_dummy_struct_L, __LINE__) { \ - char t[(expr) ? 1 : -1]; \ - } + #ifdef __COUNTER__ + #define wc_static_assert(expr) \ + struct WC_CPP_CAT4(wc_static_assert_dummy_struct_L, \ + __LINE__, _, __COUNTER__) { \ + char t[(expr) ? 1 : -1]; \ + } + #else + #define wc_static_assert(expr) \ + struct wc_static_assert_dummy_struct + #endif #ifndef wc_static_assert2 #define wc_static_assert2(expr, msg) wc_static_assert(expr) #endif