]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
lib: use static assertion to check enum values
authorDaiki Ueno <dueno@redhat.com>
Sat, 29 Feb 2020 16:01:10 +0000 (17:01 +0100)
committerDaiki Ueno <dueno@redhat.com>
Tue, 10 Mar 2020 14:30:20 +0000 (15:30 +0100)
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>
bootstrap.conf
lib/gnutls_int.h

index f47b81aa68baec7ceec22f61bc6a98ad420b5baf..8421fb995f4b59d717eaafdb0b78276324fef625 100644 (file)
@@ -28,7 +28,7 @@ required_submodules="tests/suite/tls-fuzzer/python-ecdsa tests/suite/tls-fuzzer/
 # Reproduce by: gnulib-tool --import --local-dir=gl/override --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=gl/tests --aux-dir=build-aux --with-tests --avoid=alignof-tests --avoid=lock-tests --avoid=lseek-tests --lgpl=2 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files alloca byteswap c-ctype extensions func gendocs getline gettext-h gettimeofday hash-pjw-bare havelib intprops lib-msvc-compat lib-symbol-versions maintainer-makefile manywarnings memmem-simple minmax netdb netinet_in pmccabe2html read-file secure_getenv snprintf stdint strcase strndup strtok_r strverscmp sys_socket sys_stat time_r unistd vasprintf vsnprintf warnings
 
 gnulib_modules="
-alloca byteswap c-ctype c-strcase extensions func gendocs getline gettext-h gettimeofday hash hash-pjw-bare havelib arpa_inet inet_ntop inet_pton intprops lib-msvc-compat lib-symbol-versions maintainer-makefile manywarnings memmem-simple minmax netdb netinet_in pmccabe2html read-file secure_getenv setsockopt snprintf stdint strcase strdup-posix strndup strtok_r strverscmp sys_socket sys_stat sys_types time_r unistd valgrind-tests vasprintf vsnprintf warnings
+alloca byteswap c-ctype c-strcase extensions func gendocs getline gettext-h gettimeofday hash hash-pjw-bare havelib arpa_inet inet_ntop inet_pton intprops lib-msvc-compat lib-symbol-versions maintainer-makefile manywarnings memmem-simple minmax netdb netinet_in pmccabe2html read-file secure_getenv setsockopt snprintf stdint strcase strdup-posix strndup strtok_r strverscmp sys_socket sys_stat sys_types time_r unistd valgrind-tests vasprintf verify vsnprintf warnings
 "
 
 unistring_modules="
index 058fce090c352b9c68e88f7009eb59fc76b2e2ef..4ea815997901464a82268462eed1aab7168de56a 100644 (file)
@@ -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;