-
Notifications
You must be signed in to change notification settings - Fork 128
C99 conforming static assert #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…d replace all string literal arguments to the OPENSSL_STATIC_ASSERT macro with preprocessor tokens
// and the line the assertion is defined. This should ensure name uniqueness. | ||
// The width of the bit field is set to 1 or -1, depending on the evaluation of | ||
// the boolean expression |cond|. If the condition is false, the width requested | ||
// is -1, which is illegal and would cause the compiler to throw an error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible add to an example of what an assertion error looks like? That, I think, would help in understanding the final output of all the macro definitions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing
OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
sizeof(struct aead_aes_gcm_ctx),
AEAD_state_is_too_small)
to
OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) <
sizeof(struct aead_aes_gcm_ctx),
AEAD_state_is_too_small)
produces the following compile-time error:
../crypto/../include/openssl/type_check.h:85:35: error: negative width in bit-field ‘static_assertion_at_line_913_error_is_AEAD_state_is_too_small’
unsigned int AWSLC_CONCAT(static_assertion_, msg) : (cond) ? 1 : - 1; \
^
../crypto/../include/openssl/type_check.h:83:35: note: in definition of macro ‘AWSLC_CONCAT
’
#define AWSLC_CONCAT(left, right) left##right
^~~~
../crypto/../include/openssl/type_check.h:87:53: note: in expansion of macro ‘AWSLC_STATIC_ASSERT_DEFINE’
#define AWSLC_STATIC_ASSERT_ADD_LINE0(cond, suffix) AWSLC_STATIC_ASSERT_DEFINE(cond, AWSLC_CONCAT(at_line_, suffix))
^~~~~~~~~~~~~~~~~~~~~~~~~~
../crypto/../include/openssl/type_check.h:88:59: note: in expansion of macro ‘AWSLC_STATIC_ASSERT_ADD_LINE0’
#define AWSLC_STATIC_ASSERT_ADD_LINE1(cond, line, suffix) AWSLC_STATIC_ASSERT_ADD_LINE0(cond, AWSLC_CONCAT(line, suffix))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../crypto/../include/openssl/type_check.h:89:53: note: in expansion of macro ‘AWSLC_STATIC_ASSERT_ADD_LINE1’
#define AWSLC_STATIC_ASSERT_ADD_LINE2(cond, suffix) AWSLC_STATIC_ASSERT_ADD_LINE1(cond, __LINE__, suffix)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../crypto/../include/openssl/type_check.h:90:53: note: in expansion of macro ‘AWSLC_STATIC_ASSERT_ADD_LINE2’
#define AWSLC_STATIC_ASSERT_ADD_ERROR(cond, suffix) AWSLC_STATIC_ASSERT_ADD_LINE2(cond, AWSLC_CONCAT(_error_is_, suffix))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../crypto/../include/openssl/type_check.h:91:44: note: in expansion of macro ‘AWSLC_STATIC_ASSERT_ADD_ERROR’
#define OPENSSL_STATIC_ASSERT(cond, error) AWSLC_STATIC_ASSERT_ADD_ERROR(cond, error);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../crypto/fipsmodule/cipher/e_aes.c:911:1: note: in expansion of macro ‘OPENSSL_STATIC_ASSERT’
OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) <
Issues:
CryptoAlg-611
Description of changes:
_Static_assert
andstatic_assert
are defined in C11 and does not conform to C99. This change implements a static assertion mechanism that conforms to C99. It is inspired by similar implementations in:In addition, I attempted to improve the robustness of the C99 violation test.
Main changes are in:
include/openssl/type_check.h
tests/coding_guidelines/c99_gcc_test.sh
Call-outs:
The second argument to
OPENSSL_STATIC_ASSERT
used to be a string literal. We cannot "unstringify" to construct a valid token. So, I had to replace all strings with tokens in the code base. The variable name of the typedefe'd struct should be unique to avoid shadowing already defined types.Testing:
There are no run-time changes.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.