Skip to content

Commit e1ece63

Browse files
committed
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.
1 parent 6d299ea commit e1ece63

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ __BIG_ENDIAN__
887887
__BORLANDC__
888888
__CCRX__
889889
__COMPILER_VER__
890+
__COUNTER__
890891
__CYGWIN__
891892
__DATE__
892893
__DCACHE_PRESENT

wolfssl/wolfcrypt/types.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,8 +1806,8 @@ typedef struct w64wrapper {
18061806
#define PRAGMA_DIAG_POP /* null expansion */
18071807
#endif
18081808

1809-
#define WC_CPP_CAT_(a, b) a ## b
1810-
#define WC_CPP_CAT(a, b) WC_CPP_CAT_(a, b)
1809+
#define WC_CPP_CAT4_(a, b, c, d) a ## b ## c ## d
1810+
#define WC_CPP_CAT4(a, b, c, d) WC_CPP_CAT4_(a, b, c, d)
18111811
#if defined(WC_NO_STATIC_ASSERT)
18121812
#define wc_static_assert(expr) struct wc_static_assert_dummy_struct
18131813
#define wc_static_assert2(expr, msg) wc_static_assert(expr)
@@ -1844,11 +1844,16 @@ typedef struct w64wrapper {
18441844
#define wc_static_assert2(expr, msg) _Static_assert(expr, msg)
18451845
#endif
18461846
#else
1847-
/* C89-compatible fallback */
1848-
#define wc_static_assert(expr) \
1849-
struct WC_CPP_CAT(wc_static_assert_dummy_struct_L, __LINE__) { \
1850-
char t[(expr) ? 1 : -1]; \
1851-
}
1847+
#ifdef __COUNTER__
1848+
#define wc_static_assert(expr) \
1849+
struct WC_CPP_CAT4(wc_static_assert_dummy_struct_L, \
1850+
__LINE__, _, __COUNTER__) { \
1851+
char t[(expr) ? 1 : -1]; \
1852+
}
1853+
#else
1854+
#define wc_static_assert(expr) \
1855+
struct wc_static_assert_dummy_struct
1856+
#endif
18521857
#ifndef wc_static_assert2
18531858
#define wc_static_assert2(expr, msg) wc_static_assert(expr)
18541859
#endif

0 commit comments

Comments
 (0)