Skip to content

Commit

Permalink
peer review: refactor HAVE_ANONYMOUS_INLINE_AGGREGATES and HAVE_EMPTY…
Browse files Browse the repository at this point in the history
…_AGGREGATES to conform to wolfssl convention -- defined() for true, !defined() for false -- while retaining ability for user override-off by passing in explicit 0 definition.
  • Loading branch information
douzzer committed Dec 6, 2024
1 parent 447d5ea commit 0381a47
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
16 changes: 8 additions & 8 deletions wolfssl/wolfcrypt/cryptocb.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ enum wc_CryptoCbCmdType {
/* Crypto Information Structure for callbacks */
typedef struct wc_CryptoInfo {
int algo_type; /* enum wc_AlgoType */
#if HAVE_ANONYMOUS_INLINE_AGGREGATES
#ifdef HAVE_ANONYMOUS_INLINE_AGGREGATES
union {
#endif
struct {
int type; /* enum wc_PkType */
#if HAVE_ANONYMOUS_INLINE_AGGREGATES
#ifdef HAVE_ANONYMOUS_INLINE_AGGREGATES
union {
#endif
#ifndef NO_RSA
Expand Down Expand Up @@ -279,15 +279,15 @@ typedef struct wc_CryptoInfo {
int type; /* enum wc_PqcSignatureType */
} pqc_sig_check;
#endif
#if HAVE_ANONYMOUS_INLINE_AGGREGATES
#ifdef HAVE_ANONYMOUS_INLINE_AGGREGATES
};
#endif
} pk;
#if !defined(NO_AES) || !defined(NO_DES3)
struct {
int type; /* enum wc_CipherType */
int enc;
#if HAVE_ANONYMOUS_INLINE_AGGREGATES
#ifdef HAVE_ANONYMOUS_INLINE_AGGREGATES
union {
#endif
#ifdef HAVE_AESGCM
Expand Down Expand Up @@ -375,7 +375,7 @@ typedef struct wc_CryptoInfo {
} des3;
#endif
void* ctx;
#if HAVE_ANONYMOUS_INLINE_AGGREGATES
#ifdef HAVE_ANONYMOUS_INLINE_AGGREGATES
};
#endif
} cipher;
Expand All @@ -387,7 +387,7 @@ typedef struct wc_CryptoInfo {
const byte* in;
word32 inSz;
byte* digest;
#if HAVE_ANONYMOUS_INLINE_AGGREGATES
#ifdef HAVE_ANONYMOUS_INLINE_AGGREGATES
union {
#endif
#ifndef NO_SHA
Expand All @@ -409,7 +409,7 @@ typedef struct wc_CryptoInfo {
wc_Sha3* sha3;
#endif
void* ctx;
#if HAVE_ANONYMOUS_INLINE_AGGREGATES
#ifdef HAVE_ANONYMOUS_INLINE_AGGREGATES
};
#endif
} hash;
Expand Down Expand Up @@ -454,7 +454,7 @@ typedef struct wc_CryptoInfo {
void *ctx;
} cmd;
#endif
#if HAVE_ANONYMOUS_INLINE_AGGREGATES
#ifdef HAVE_ANONYMOUS_INLINE_AGGREGATES
};
#endif
} wc_CryptoInfo;
Expand Down
32 changes: 22 additions & 10 deletions wolfssl/wolfcrypt/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,36 +123,49 @@ decouple library dependencies with standard string, memory and so on.

#ifndef HAVE_ANONYMOUS_INLINE_AGGREGATES
/* if a version is available, pivot on the version, otherwise guess it's
* allowed, subject to override.
* disallowed, subject to override.
*/
#if !defined(WOLF_C89) && (!defined(__STDC__) \
|| (!defined(__STDC_VERSION__) && !defined(__cplusplus)) \
|| (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201101L)) \
|| (defined(__cplusplus) && (__cplusplus >= 201103L)))
#define HAVE_ANONYMOUS_INLINE_AGGREGATES 1
#else
#define HAVE_ANONYMOUS_INLINE_AGGREGATES 0
#endif
#elif ~(~HAVE_ANONYMOUS_INLINE_AGGREGATES + 1) == 1
/* forced on with empty value -- remap to 1 */
#undef HAVE_ANONYMOUS_INLINE_AGGREGATES
#define HAVE_ANONYMOUS_INLINE_AGGREGATES 1
#elif HAVE_ANONYMOUS_INLINE_AGGREGATES
/* forced on with explicit nonzero value -- leave as-is. */
#else
/* forced off with explicit zero value -- remap to undef. */
#undef HAVE_ANONYMOUS_INLINE_AGGREGATES
#endif

#ifndef HAVE_EMPTY_AGGREGATES
/* The C standards don't define empty aggregates, but gcc and clang do.
* We need to accommodate them for one of the same reasons C++ does --
* templates that conditionally result in empty aggregates, e.g. in
* hash.h.
* conditionally empty aggregates, e.g. in hash.h.
*/
#if !defined(WOLF_C89) && defined(__GNUC__) && \
!defined(__STRICT_ANSI__) && \
HAVE_ANONYMOUS_INLINE_AGGREGATES + 0 == 1
defined(HAVE_ANONYMOUS_INLINE_AGGREGATES)
#define HAVE_EMPTY_AGGREGATES 1
#else
#define HAVE_EMPTY_AGGREGATES 0
#endif
#elif ~(~HAVE_EMPTY_AGGREGATES + 1) == 1
/* forced on with empty value -- remap to 1 */
#undef HAVE_EMPTY_AGGREGATES
#define HAVE_EMPTY_AGGREGATES 1
#elif HAVE_EMPTY_AGGREGATES
/* forced on with explicit nonzero value -- leave as-is. */
#else
/* forced off with explicit zero value -- remap to undef. */
#undef HAVE_EMPTY_AGGREGATES
#endif

#define _WOLF_AGG_DUMMY_MEMBER_HELPER2(a, b, c) a ## b ## c
#define _WOLF_AGG_DUMMY_MEMBER_HELPER(a, b, c) _WOLF_AGG_DUMMY_MEMBER_HELPER2(a, b, c)
#if HAVE_EMPTY_AGGREGATES + 0 == 1
#ifdef HAVE_EMPTY_AGGREGATES
/* swallow the semicolon with a zero-sized array (language extension
* specific to gcc/clang).
*/
Expand Down Expand Up @@ -185,7 +198,6 @@ decouple library dependencies with standard string, memory and so on.
* without disrupting clean flow/syntax when some enum values are
* preprocessor-gated.
*/
#define WC_VALUE_OF(x) x
#if defined(WOLF_C89) || defined(WOLF_NO_TRAILING_ENUM_COMMAS)
#define _WOLF_ENUM_DUMMY_LAST_ELEMENT_HELPER2(a, b, c, d, e) a ## b ## c ## d ## e
#define _WOLF_ENUM_DUMMY_LAST_ELEMENT_HELPER(a, b, c, d, e) _WOLF_ENUM_DUMMY_LAST_ELEMENT_HELPER2(a, b, c, d, e)
Expand Down

0 comments on commit 0381a47

Please sign in to comment.