diff options
author | Grant Erickson <gerickson@nuovations.com> | 2024-03-28 10:25:46 -0700 |
---|---|---|
committer | Denis Kenzior <denkenz@gmail.com> | 2024-03-28 17:30:22 -0500 |
commit | d519d3035742e7ddc8c0172b48e6655323f66a3c (patch) | |
tree | ac8fd01a232ab0ef773aaad75dfed28e42a0784f | |
parent | 57fcbf80bff6fdbf10b21bd4d3f3e282f14599b9 (diff) |
util: Add portable casting preprocessor macros.
ELL itself is a pure C source-based project and is compiled as such.
However, if ELL headers and interfaces end up getting integrated with
and compiled in a C++ environment, certain assignments within ELL inline
header functions may be flagged by the C++ compiler as errors or
warnings.
The added casting preprocessor macros are generally useful across any
source based that might want to leverage them from ELL. However, within
ELL, they should be used in inline header functions where such functions
may be consumed in either a C or C++ environment with a towards
L_PERMISSIVE_CAST where possible. Since L_PERMISSIVE_CAST is not a cast
at all in C, it will not mask otherwise-legitimate warnings in that
environment.
-rw-r--r-- | ell/util.h | 33 |
1 files changed, 28 insertions, 5 deletions
@@ -43,6 +43,27 @@ _Pragma("GCC diagnostic pop") \ r; \ }) +/* + * If ELL headers and iterfaces end up getting compiled in a C++ + * environment, even though ELL itself is a C source based and is + * compiled as such, certain assignments may be flagged by the C++ + * compiler as errors or warnings. The following portable casts should + * be used in such cases, with a preference towards L_PERMISSIVE_CAST + * where possible since it is not a cast in C and, therefore, will not + * mask otherwise-legitimate warnings in that environment. + */ +#ifdef __cplusplus +#define L_CONST_CAST(t, v) const_cast<t>(v) +#define L_REINTERPRET_CAST(t, v) reinterpret_cast<t>(v) +#define L_STATIC_CAST(t, v) static_cast<t>(v) +#define L_PERMISSIVE_CAST(t, v) L_STATIC_CAST(t, v) +#else +#define L_CONST_CAST(t, v) ((t)(v)) +#define L_REINTERPRET_CAST(t, v) ((t)(v)) +#define L_STATIC_CAST(t, v) ((t)(v)) +#define L_PERMISSIVE_CAST(t, v) (v) +#endif + #define L_PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p))) #define L_UINT_TO_PTR(u) ((void *) ((uintptr_t) (u))) @@ -381,8 +402,10 @@ inline __attribute__((always_inline)) void _l_close_cleanup(void *p) static inline int l_secure_memcmp(const void *a, const void *b, size_t size) { - const volatile uint8_t *aa = a; - const volatile uint8_t *bb = b; + const volatile uint8_t *aa = + L_PERMISSIVE_CAST(const volatile uint8_t *, a); + const volatile uint8_t *bb = + L_PERMISSIVE_CAST(const volatile uint8_t *, b); int res = 0, diff, mask; /* @@ -433,9 +456,9 @@ static inline void l_secure_select(bool select_left, const void *left, const void *right, void *out, size_t len) { - const uint8_t *l = left; - const uint8_t *r = right; - uint8_t *o = out; + const uint8_t *l = L_PERMISSIVE_CAST(const uint8_t *, left); + const uint8_t *r = L_PERMISSIVE_CAST(const uint8_t *, right); + uint8_t *o = L_PERMISSIVE_CAST(uint8_t *, out); uint8_t mask = -(!!select_left); size_t i; |