wolfssl
wolfssl copied to clipboard
[Bug]: wc_static_assert used in header files breaks MSVC/VS2019 C++ compilation
Contact Details
Version
5.7.6
Description
First reported in https://jira.mariadb.org/browse/MDEV-36054
Does not happen on MSVC included into VS2022
D:\a\mroonga\mroonga\mariadb\extra\wolfssl\wolfssl\wolfssl/wolfcrypt/error-crypt.h(321,56): error C2429: language feature 'terse static assert' requires compiler flag '/std:c++17'
wc_static_assert((int)WC_LAST_E <= (int)WC_SPAN2_LAST_E); ^ D:\a\mroonga\mroonga\mariadb\extra\wolfssl\wolfssl\wolfssl/wolfcrypt/error-crypt.h(322,51): error C2429: language feature 'terse static assert' requires compiler flag '/std:c++17' wc_static_assert((int)MIN_CODE_E <= (int)WC_LAST_E); ^ D:\a\mroonga\mroonga\mariadb\extra\wolfssl\wolfssl\wolfssl/wolfcrypt/error-crypt.h(323,61): error C2429: language feature 'terse static assert' requires compiler flag '/std:c++17' wc_static_assert((int)MIN_CODE_E <= (int)WC_SPAN2_MIN_CODE_E); ^ D:\a\mroonga\mroonga\mariadb\extra\wolfssl\wolfssl\wolfssl/error-ssl.h(244,55): error C2429: language feature 'terse static assert' requires compiler flag '/std:c++17' wc_static_assert((int)WC_LAST_E <= (int)WOLFSSL_LAST_E); ^
Reproduction steps
compile a C++ file that uses wolfssl 5.7.4, using MSVC included into Visual Studio 2019 without /std:c++17
Relevant log output
Hi @vaintroub ,
We have had issues with static_assert on MSVC/Visual Studio in the past, these issues were resolved in our most recent release 5.7.6. Please try upgrading and let me know if it helps.
hi @kareem-wolfssl , sorry my mistake. We use the latest 5.7.6, and this error stems directly from it. I have corrected the version now
Yes, about the issue - my humble opinion is that - maybe wc_static_asserts do not belong to public WolfSSL headers, because now you have to think, whether wc_assert is included into C++ source, which version of C++ is it, whether this version of C++ supports static_asserts with 1 or 2 arguments, which version of compiler is used and and so on.
It is not tested well on your CI, since WolfSSL mostly uses pure C, including the example programs.
Yes, the idea behind wc_assert is to catch errors during wolfssl compilation, not to cause errors for product that includes public headers.
Just my interpretation, I could be wrong of course. Anyway, it causes errors in some environments, as I reported.
@vaintroub , you can compile with WC_NO_STATIC_ASSERT defined in your CFLAGS or CPPFLAGS to confirm there are no other problems with the wolfSSL headers.
Do you have a particular test clause in mind to detect and accommodate the toolchain version where the problem arise?
By the way, I can't make sense of the C2429 error code you're seeing. The compiler documentation says
If the compiler is in the default C++14 mode and
/permissive-or/Zc:static_assertis specified, it uses/Zc:static_assertbehavior. However, if it evaluates astatic_assertin a template body, it also reports off-by-default warning C5254, "language feature 'terse static assert' requires compiler flag '/std:c++17'", since this behavior isn't required until C++17.
(https://learn.microsoft.com/en-us/cpp/build/reference/zc-static-assert?view=msvc-170)
So the expected code here is C5254, and it is off by default.
C2429 appears to be something completely different and unrelated (https://learn.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/compiler-error-c2429?view=msvc-170).
Your comments regarding the complicated logic around wc_static_assert() in the wolfSSL header are well taken. The setup for wc_static_assert() would be better in wc_port.h, but it needs to appear somewhere in the headers, and it needs to work correctly on all supported targets, even if only with dummy definitions that disable the static asserts.
I'm reporting what I have seen, and what other people have seen (MariaDB users see the problem, while we have not, by using a later version of the compiler). I see it with the latest VS2019, and do not see it with VS2022, as I already reported.
The warning of course makes sense, because you're using C++ static_assert with one argument, if C++ file includes wolfssl header. It actually needs C++17. We compile in C++11 mode, with
/permissive- . C2429 is accurate in that says "The language feature requires a specific compiler option for support". It is a generic error, applicable for multiple language features. The example they give is for language feature called "'nested-namespace-definition" , and what we got here is another language feature called "terse static assert" . I do not know why VS2022 does not complain, I would if I was VS2022.
@vaintroub try https://github.com/wolfSSL/wolfssl/pull/8440
This static assert causes warnings with clang-cl compiler (Clang acting as-if it would be MSVC), which is probably not tested by suite.
In file included from E:\10.11\extra\wolfssl\wolfssl\wolfcrypt\src\error.c:29: E:\10.11\extra\wolfssl\wolfssl\wolfssl/wolfcrypt/error-crypt.h(321,1): warning: use of 'static_assert' without inclusion of <assert.h> is a Microsoft extension [-Wmicrosoft-static-assert] 321 | wc_static_assert((int)WC_LAST_E <= (int)WC_SPAN2_LAST_E);
There are thousands of such warnings. This is to my point, that static asserts probably do not belong to static headers. IMO.