diff options
author | Daiki Ueno <dueno@redhat.com> | 2020-02-29 17:01:10 +0100 |
---|---|---|
committer | Daiki Ueno <dueno@redhat.com> | 2020-03-10 15:30:20 +0100 |
commit | d3ab18bbbdffc5e48df2054114f222ffb82af883 (patch) | |
tree | 43ceec27308bc71248ec06b16aa0d53d83b4bc1d /lib/gnutls_int.h | |
parent | 5937fe57a8dea3298963247c0399749d7065eaf2 (diff) | |
download | gnutls-tmp-static-assert.tar.gz |
lib: use static assertion to check enum valuestmp-static-assert
We previously had checks of enum values with '#if', such as below:
#define GNUTLS_EXTENSION_MAX_VALUE 31
typedef enum extensions_t {
...
GNUTLS_EXTENSION_MAX /* not real extension - used for iterators */
} extensions_t;
/* we must provide at least 16 extensions for users to register */
#if GNUTLS_EXTENSION_MAX_VALUE - GNUTLS_EXTENSION_MAX < 16
# error not enough extension types
#endif
This doesn't work as expected; because GNUTLS_EXTENSION_MAX is not
defined as a preprocessor macro, it always expands to 0. To properly
do this check, we need to use static assert as provided as the
'verify' macro in gnulib.
Signed-off-by: Daiki Ueno <dueno@redhat.com>
Diffstat (limited to 'lib/gnutls_int.h')
-rw-r--r-- | lib/gnutls_int.h | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/lib/gnutls_int.h b/lib/gnutls_int.h index 058fce090c..4ea8159979 100644 --- a/lib/gnutls_int.h +++ b/lib/gnutls_int.h @@ -361,19 +361,21 @@ typedef enum extensions_t { #define GNUTLS_EXTENSION_MAX_VALUE 63 #define ext_track_t uint64_t -#if GNUTLS_EXTENSION_MAX >= GNUTLS_EXTENSION_MAX_VALUE -# error over limit -#endif +#include <verify.h> -#if GNUTLS_EXTENSION_MAX >= MAX_EXT_TYPES -# error over limit -#endif +verify(GNUTLS_EXTENSION_MAX < GNUTLS_EXTENSION_MAX_VALUE); +verify(GNUTLS_EXTENSION_MAX < MAX_EXT_TYPES); -/* we must provide at least 16 extensions for users to register */ -#if GNUTLS_EXTENSION_MAX_VALUE - GNUTLS_EXTENSION_MAX < 16 -# error not enough extension types; increase GNUTLS_EXTENSION_MAX_VALUE, MAX_EXT_TYPES and used_exts type -#endif +/* we must provide at least 16 extensions for users to register; + * increase GNUTLS_EXTENSION_MAX_VALUE, MAX_EXT_TYPES and used_exts + * type if this fails + */ +verify(GNUTLS_EXTENSION_MAX_VALUE - GNUTLS_EXTENSION_MAX >= 16); +/* The 'verify' symbol from <verify.h> is used extensively in the + * code; undef it to avoid clash + */ +#undef verify typedef enum { CIPHER_STREAM, CIPHER_BLOCK, CIPHER_AEAD } cipher_type_t; |