Skip to content

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

Merged
merged 9 commits into from
Dec 22, 2020
Merged

Conversation

torben-hansen
Copy link
Contributor

@torben-hansen torben-hansen commented Dec 11, 2020

Issues:

CryptoAlg-611

Description of changes:

_Static_assert and static_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.

dkostic
dkostic previously approved these changes Dec 16, 2020
// 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.
Copy link
Contributor

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.

Copy link
Contributor Author

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) <

@bryce-shang bryce-shang merged commit 4702a85 into aws:main Dec 22, 2020
@bryce-shang bryce-shang mentioned this pull request Feb 19, 2021
bryce-shang added a commit to bryce-shang/aws-lc that referenced this pull request Apr 15, 2021
bryce-shang added a commit to bryce-shang/aws-lc that referenced this pull request Apr 15, 2021
bryce-shang added a commit to bryce-shang/aws-lc that referenced this pull request Apr 15, 2021
Add ecdsa kat test as unit test.

Apply Nevine's new asm code.

Change syntax aws#64+16 to aws#80.

Edit bash file.
bryce-shang added a commit to bryce-shang/aws-lc that referenced this pull request Apr 15, 2021
Add ecdsa kat test as unit test.

Apply Nevine's new asm code.

Change syntax aws#64+16 to aws#80.

Edit bash file.
bryce-shang added a commit to bryce-shang/aws-lc that referenced this pull request Apr 15, 2021
Add ecdsa kat test as unit test.

Apply Nevine's new asm code.

Change syntax aws#64+16 to aws#80.

Edit bash file.
bryce-shang added a commit to bryce-shang/aws-lc that referenced this pull request Apr 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants