-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Warnings from GCC 15 -Wunterminated-string-initialization #9944
Comments
I have patches if you want them. |
There is no undefined behavior here. In this particular case, adding a null byte would not be compliant to the protocol specification.
Note — only with GCC 15, which is not released yet. GCC 15's new warning We do aim to compile with no warnings at a reasonable warning level with common compilers. This will include GCC 15 with Patches to avoid triggering the warning would be welcome. But they obviously need to be correct! And in most cases, for this warning, in our code base, the warning is a false positive. So we need to find a way to let GCC know that the array is intended to be a byte array and not a null-terminated string. |
I understand the intention of the code. This wasn't discussed in C89 (undefined bahavior), but the C99 spec did cover it:
We should file a bug to GCC if it's not filed yet. |
I would like to point out though, the original author wasn't so sure either of the behavior on this:
|
This is well-defined behavior in all versions of the C language, even back to K&R C AFAIR. Using a string literal as an initializer adds a null byte if there is room. The array size can be left empty, in which case the size includes a terminating null byte. The following examples illustrate array initialization (hopefully I didn't make any copypasta). All are fully defined in all versions of the C language.
The warning from One way to avoid the warning would be to use braced initializers instead of string literals. However, here, it would mean that the strings from the TLS specification wouldn't appear in the code, which is not good. If we choose to do that, we should keep the string in a comment. But that comes with a risk of typos in the comment. (Typos in the code would be caught immediately by interoperability tests.) Fortunately we don't add or modify such code often. |
I even pulled out my KR C book, "If its size is fixed, the number of characters in the string, not counting the terminating null character, must not exceed the size of the array". I am actually surprised by all of these being defined, fun little endeavor and I learned a new part of C. |
I thought this was undefined behavior, but K+R does cover it. #9944 (comment).
Yes, I don't disagree that the behavior was intentional (it was commented as such), I'm just thinking if I set the size field, why warn me? as this is an intentional use. I am trying to deduce one of the following:
If it's 2 or 3, I would file a bug with GCC to remove it from the default set as being overly pedantic. If it's 1 then I need to find out why Fedora added it and see if folks can opt out. |
It's (2) and (3). Our CI enforces a clean build with I don't know what the best fix is. In test code, using braced initializers or putting a null terminator in the test data would be ok. (Although I slightly dislike having test data that happens to be a null-terminated string where a byte string is expected, because that can hide bugs if the code somehow starts using a string function and thus stops working on arbitrary byte strings.) In the TLS code, it's easier to understand the code if the strings from the protocol definition appear in a text search, and the strings are byte strings and we don't particularly want to waste memory and effort to put a null byte at the end but process only the non-null part. This makes a warning-off pragma look attractive, but I'm not sure if that's possible with the way the macros are structured. |
Summary
Some of the string literals use the size of the array to specify an array of size N where N is the length of the string and so things like sizeof() will truncate the NULL byte.
Warning message Example:
Code Examples to reproduce:
This waring can be enabled via:
-Wunterminated-string-initialization
I have attached the build log.
build.log
System information
Mbed TLS version (number or commit id): 3.6.2
Operating system and version: F32 with gcc-15
Configuration (if not default, please attach
mbedtls_config.h
):Compiler and options (if you used a pre-built binary, please indicate how you obtained it):
Additional environment information:
Expected behavior
Builds
Actual behavior
Steps to reproduce
Additional information
The text was updated successfully, but these errors were encountered: