Skip to content

Commit c230e10

Browse files
authored
Merge pull request #8044 from douzzer/20241004-wc_static_assert
20241004-wc_static_assert
2 parents 4962180 + e944967 commit c230e10

7 files changed

Lines changed: 47 additions & 54 deletions

File tree

src/dtls13.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ typedef struct Dtls13HandshakeHeader {
7171
byte fragmentLength[3];
7272
} Dtls13HandshakeHeader;
7373

74-
static_assert(sizeof(Dtls13HandshakeHeader) == DTLS13_HANDSHAKE_HEADER_SZ);
74+
wc_static_assert(sizeof(Dtls13HandshakeHeader) == DTLS13_HANDSHAKE_HEADER_SZ);
7575

7676
/**
7777
* struct Dtls13Recordplaintextheader: represent header of unprotected DTLSv1.3

src/ssl_crypto.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2923,8 +2923,7 @@ void wolfSSL_DES_ecb_encrypt(WOLFSSL_DES_cblock* in, WOLFSSL_DES_cblock* out,
29232923
static int wolfssl_aes_set_key(const unsigned char *key, const int bits,
29242924
AES_KEY *aes, int enc)
29252925
{
2926-
typedef char aes_test[sizeof(AES_KEY) >= sizeof(Aes) ? 1 : -1];
2927-
(void)sizeof(aes_test);
2926+
wc_static_assert(sizeof(AES_KEY) >= sizeof(Aes));
29282927

29292928
/* Validate parameters. */
29302929
if ((key == NULL) || (aes == NULL)) {
@@ -3438,8 +3437,7 @@ size_t wolfSSL_CRYPTO_cts128_decrypt(const unsigned char *in,
34383437
void wolfSSL_RC4_set_key(WOLFSSL_RC4_KEY* key, int len,
34393438
const unsigned char* data)
34403439
{
3441-
typedef char rc4_test[sizeof(WOLFSSL_RC4_KEY) >= sizeof(Arc4) ? 1 : -1];
3442-
(void)sizeof(rc4_test);
3440+
wc_static_assert(sizeof(WOLFSSL_RC4_KEY) >= sizeof(Arc4));
34433441

34443442
WOLFSSL_ENTER("wolfSSL_RC4_set_key");
34453443

wolfcrypt/src/evp.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10495,21 +10495,16 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD* type)
1049510495
const WOLFSSL_EVP_MD* md)
1049610496
{
1049710497
int ret = WOLFSSL_SUCCESS;
10498+
#ifdef WOLFSSL_ASYNC_CRYPT
10499+
wc_static_assert(WC_ASYNC_DEV_SIZE >= sizeof(WC_ASYNC_DEV));
10500+
#endif
1049810501

1049910502
WOLFSSL_ENTER("EVP_DigestInit");
1050010503

1050110504
if (ctx == NULL) {
1050210505
return WOLFSSL_FAILURE;
1050310506
}
1050410507

10505-
10506-
#ifdef WOLFSSL_ASYNC_CRYPT
10507-
/* compile-time validation of ASYNC_CTX_SIZE */
10508-
typedef char async_test[WC_ASYNC_DEV_SIZE >= sizeof(WC_ASYNC_DEV) ?
10509-
1 : -1];
10510-
(void)sizeof(async_test);
10511-
#endif
10512-
1051310508
/* Set to 0 if no match */
1051410509
ctx->macType = EvpMd2MacType(md);
1051510510
if (md == NULL) {

wolfcrypt/test/test.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -841,11 +841,11 @@ static void render_error_message(const char* msg, wc_test_ret_t es)
841841
* stores an error string in the supplied buffer. this is all most
842842
* infelicitous...
843843
*/
844-
#if !defined(STRING_USER) && !defined(NO_ERROR_STRINGS) && \
844+
#if !defined(STRING_USER) && !defined(NO_ERROR_STRINGS) && \
845845
(defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199901L)) && \
846-
((defined(__GLIBC__) && (__GLIBC__ >= 2)) || \
847-
(defined(__USE_XOPEN2K) && \
848-
defined(_POSIX_C_SOURCE) && \
846+
((defined(__GLIBC__) && (__GLIBC__ >= 2) && defined(__USE_GNU)) || \
847+
(defined(__USE_XOPEN2K) && \
848+
defined(_POSIX_C_SOURCE) && \
849849
(_POSIX_C_SOURCE >= 200112L)))
850850

851851
char errno_buf[64], *errno_string;

wolfssl/internal.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,18 +2071,9 @@ enum Misc {
20712071

20722072
#define MAX_ENCRYPT_SZ ENCRYPT_LEN
20732073

2074-
/* A static check to assert a relation between x and y */
2075-
#define WOLFSSL_ASSERT_TEST(x, y, op) do { \
2076-
typedef char _args_test_[(x) op (y) ? 1 : -1]; \
2077-
(void)sizeof(_args_test_); \
2078-
} while(0)
2074+
#define WOLFSSL_ASSERT_EQ(x, y) wc_static_assert((x) == (y))
20792075

2080-
#define WOLFSSL_ASSERT_EQ(x, y) WOLFSSL_ASSERT_TEST(x, y, ==)
2081-
2082-
#define WOLFSSL_ASSERT_SIZEOF_TEST(x, y, op) \
2083-
WOLFSSL_ASSERT_TEST(sizeof(x), sizeof(y), op)
2084-
2085-
#define WOLFSSL_ASSERT_SIZEOF_GE(x, y) WOLFSSL_ASSERT_SIZEOF_TEST(x, y, >=)
2076+
#define WOLFSSL_ASSERT_SIZEOF_GE(x, y) wc_static_assert(sizeof(x) >= sizeof(y))
20862077

20872078
/* states. Adding state before HANDSHAKE_DONE will break session importing */
20882079
enum states {

wolfssl/wolfcrypt/settings.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,17 @@
2020
*/
2121

2222
/*
23-
* ************************************************************************
23+
* Note, this file should not be edited to activate/deactivate features.
2424
*
25-
* ******************************** NOTICE ********************************
26-
*
27-
* ************************************************************************
28-
*
29-
* This method of uncommenting a line in settings.h is outdated.
30-
*
31-
* Please use user_settings.h / WOLFSSL_USER_SETTINGS
25+
* Instead, add/edit user_settings.h, and compile with -DWOLFSSL_USER_SETTINGS
3226
*
3327
* or
3428
*
35-
* ./configure CFLAGS="-DFLAG"
29+
* ./configure CFLAGS="-DFEATURE_FLAG_TO_DEFINE -UFEATURE_FLAG_TO_CLEAR [...]"
3630
*
3731
* For more information see:
3832
*
3933
* https://www.wolfssl.com/how-do-i-manage-the-build-configuration-of-wolfssl/
40-
*
4134
*/
4235

4336

wolfssl/wolfcrypt/types.h

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,33 +1695,49 @@ typedef struct w64wrapper {
16951695

16961696
#define WC_CPP_CAT_(a, b) a ## b
16971697
#define WC_CPP_CAT(a, b) WC_CPP_CAT_(a, b)
1698-
#if (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
1699-
(defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L))
1700-
#ifndef static_assert2
1701-
#define static_assert2 static_assert
1702-
#endif
1703-
#elif !defined(static_assert)
1704-
#if !defined(__cplusplus) && \
1698+
#if defined(WC_NO_STATIC_ASSERT)
1699+
#define wc_static_assert(expr) struct wc_static_assert_dummy_struct
1700+
#define wc_static_assert2(expr, msg) wc_static_assert(expr)
1701+
#elif !defined(wc_static_assert)
1702+
#if (defined(__cplusplus) && (__cplusplus >= 201703L)) || \
1703+
(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 202311L)) || \
1704+
(defined(_MSVC_LANG) && (_MSVC_LANG >= 201103L))
1705+
/* native variadic static_assert() */
1706+
#define wc_static_assert static_assert
1707+
#ifndef wc_static_assert2
1708+
#define wc_static_assert2 static_assert
1709+
#endif
1710+
#elif defined(_MSC_VER) && (__STDC_VERSION__ >= 201112L)
1711+
/* native 2-argument static_assert() */
1712+
#define wc_static_assert(expr) static_assert(expr, #expr)
1713+
#ifndef wc_static_assert2
1714+
#define wc_static_assert2(expr, msg) static_assert(expr, msg)
1715+
#endif
1716+
#elif !defined(__cplusplus) && \
17051717
!defined(__STRICT_ANSI__) && \
17061718
!defined(WOLF_C89) && \
17071719
defined(__STDC_VERSION__) && \
17081720
(__STDC_VERSION__ >= 201112L) && \
17091721
((defined(__GNUC__) && \
17101722
(__GNUC__ >= 5)) || \
17111723
defined(__clang__))
1712-
#define static_assert(expr) _Static_assert(expr, #expr)
1713-
#ifndef static_assert2
1714-
#define static_assert2(expr, msg) _Static_assert(expr, msg)
1724+
/* native 2-argument _Static_assert() */
1725+
#define wc_static_assert(expr) _Static_assert(expr, #expr)
1726+
#ifndef wc_static_assert2
1727+
#define wc_static_assert2(expr, msg) _Static_assert(expr, msg)
17151728
#endif
17161729
#else
1717-
#define static_assert(expr) \
1718-
struct WC_CPP_CAT(wc_dummy_struct_L, __LINE__)
1719-
#ifndef static_assert2
1720-
#define static_assert2(expr, msg) static_assert(expr)
1730+
/* C89-compatible fallback */
1731+
#define wc_static_assert(expr) \
1732+
struct WC_CPP_CAT(wc_static_assert_dummy_struct_L, __LINE__) { \
1733+
char t[(expr) ? 1 : -1]; \
1734+
}
1735+
#ifndef wc_static_assert2
1736+
#define wc_static_assert2(expr, msg) wc_static_assert(expr)
17211737
#endif
17221738
#endif
1723-
#elif !defined(static_assert2)
1724-
#define static_assert2(expr, msg) static_assert(expr)
1739+
#elif !defined(wc_static_assert2)
1740+
#define wc_static_assert2(expr, msg) wc_static_assert(expr)
17251741
#endif
17261742

17271743
#ifndef SAVE_VECTOR_REGISTERS

0 commit comments

Comments
 (0)