-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[3.6] Fix GCC 15 warning 'Wunterminated-string-initialization' #10215
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
…ization warning Signed-off-by: Felix Conway <felix.conway@arm.com>
library/ssl_tls13_keys.c
Outdated
|
|
||
| static const char tls13_label_prefix[6] = "tls13 "; | ||
| /* We need to tell the compiler that we meant to leave out the null character. */ | ||
| static const char tls13_label_prefix[6] __attribute__ ((nonstring)) = "tls13 "; |
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.
__attribute__ is specific to GCC and GCC-like compilers such as Clang. And this attribute might not exist in older versions that we still care about, which we can check for with __has_attribute. So we need something like
#if __GCC__ && defined(__has_attribute) && __has_attribute()__non_string__)
#define MBEDTLS_ATTRIBUTE_STRING_IS_BINARY __attribute__(__nonstring__)
#else
#define MBEDTLS_ATTRIBUTE_STRING_IS_BINARY
#endif
Probably in tf-psa-crypto/core/common.h or some such.
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.
You shouldn't use defined(__has_attribute) && __has_attribute() in the same expression, because if the LHS of the && evaluates to false, the preprocessor still needs to be able to parse the RHS which it won't. Proper usage of __has_attribute is as follows:
#if defined(__has_attribute)
#if __has_attribute(__nonstring__)
#define MBEDTLS_HAS_ATTRIBUTE_NONSTRING
#endif
#endif
#if MBEDTLS_HAS_ATTRIBUTE_NONSTRING
#define MBEDTLS_ATTRIBUTE_STRING_IS_BINARY __attribute__(__nonstring__)
#else
#define MBEDTLS_ATTRIBUTE_STRING_IS_BINARY
#endifThere 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.
(Btw this is what the GCC documentation, which you linked to, says.)
…MINATED_STRING This macro applies __attribute__((nonstring)) when the compiler supports it Signed-off-by: Felix Conway <felix.conway@arm.com>
|
Using non-standard keywords overcomplicates the matter for a handful of small arrays. |
|
A downside of using Also, using a macro has the advantage that if a reader wonders why it's there, they can just look at the definition of the macro. We don't need to repeat a long explanation about strings and warnings in many places. |
|
There is no reason grep would fail to find this: |
Signed-off-by: Felix Conway <felix.conway@arm.com>
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.
LGTM except the changelog entry.
I've checked locally that #10215 plus Mbed-TLS/mbedtls-framework#173 results in no warning with GCC 15 in the full config.
| * GCC 15 introduced the warning 'unterminated-string-initialization', which | ||
| complains if you initialize a string into an array without space for a | ||
| terminating null character. This is intentional in many parts of the | ||
| code, so suppress the warning in these places. Fixes #9944. |
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.
This is both longer than it needs (we don't need to explain the details of what the warning is about), and a bit too non-committal. We should claim that we've silenced the warning. (We don't test every configuration with GCC 15, so we may have missed one, but since we're testing the full config plus drivers, it's unlikely that something slipped through the crack.)
Suggested wording, partly based on prior changelog entries:
Silence spurious -Wunterminated-string-initialization warnings introduced by GCC 15. Fixes #9944.
(We've gone as short as “Fix IAR compiler warnings.”, but I think that was too extreme on the short side.)
mpg
left a comment
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.
LGTM, thanks!
That's a good intermediate step but in the end we want that checked by the CI, so I think we need an extra PR once everything is merged to update
Wdyt @felixc-arm ? |
|
Sure, makes sense to me - I can put up those (three? - I guess |
Signed-off-by: Felix Conway <felix.conway@arm.com>
|
Hopefully we'll merge 3.6 and crypto this morning, let's at least wait until that's done. The follow-up PR will be a submodule update plus a very simple patch, so we won't really save time by preparing it in advance. (We'd save risk to have a positive CI result, but the risk is low and we'd have to wait for the CI anyway.) |
gilles-peskine-arm
left a comment
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.
LGTM
mpg
left a comment
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.
LGTM, thanks!
|
The issue was not the warning itself, but "too many initializer values" as in |
|
@irwir Sorry, where is that? We're not silencing any warning about having too many initializer values, and it's not popping up. |
|
The root of the issue is in the fact that sizeof("abc") is 4, not 3. To close my comments here, there should have been no macros, no explanations of compiler's weirdness in changes.log, but real elimination of the cause instead.
|
|
@irwir Because string literals have a null terminator in every other context, this is a gotcha in the C language. So it makes sense that some compilers warn about it. But the behavior is well-defined and has always been. We know about this behavior and rely on it, so it's natural that we tell the compiler that we know what we're doing. |
d593c54
mbedtls-3.6 part of #9944
Merge order (for 3.6):
Merge this PR
Once this PR + the
mbedtlsdevelopment (#10216) andTF-PSA-Crypto(Mbed-TLS/TF-PSA-Crypto#313) PRs have been merged, then merge the framework PR (Mbed-TLS/mbedtls-framework#173)This PR does not depend on the framework PR Mbed-TLS/mbedtls-framework#173 in terms of submodule updates, but will require it to be merged to fix the issue completely. AIUI during the release process the framework pointer is updated,
so I haven't done it here on the assumption that it will be done later, but I can change this to ensure the framework change gets pulled in when this is merged.so if we want the issue to be resolved before the release process starts, then there will need to be an extra commit updating the framework pointer once the framework PR is merged.PR checklist