Skip to content

Commit d593c54

Browse files
authored
Merge pull request #10215 from felixc-arm/gcc-15-warning-3.6
[3.6] Fix GCC 15 warning 'Wunterminated-string-initialization'
2 parents 4c26d7d + b8d1473 commit d593c54

File tree

7 files changed

+35
-8
lines changed

7 files changed

+35
-8
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Bugfix
2+
* Silence spurious -Wunterminated-string-initialization warnings introduced
3+
by GCC 15. Fixes #9944.

library/common.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,20 @@ static inline void mbedtls_xor_no_simd(unsigned char *r,
434434
# define MBEDTLS_MAYBE_UNUSED
435435
#endif
436436

437+
/* GCC >= 15 has a warning 'unterminated-string-initialization' which complains if you initialize
438+
* a string into an array without space for a terminating NULL character. In some places in the
439+
* codebase this behaviour is intended, so we add the macro MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING
440+
* to suppress the warning in these places.
441+
*/
442+
#if defined(__has_attribute)
443+
#if __has_attribute(nonstring)
444+
#define MBEDTLS_HAS_ATTRIBUTE_NONSTRING
445+
#endif /* __has_attribute(nonstring) */
446+
#endif /* __has_attribute */
447+
#if defined(MBEDTLS_HAS_ATTRIBUTE_NONSTRING)
448+
#define MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING __attribute__((nonstring))
449+
#else
450+
#define MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING
451+
#endif /* MBEDTLS_HAS_ATTRIBUTE_NONSTRING */
452+
437453
#endif /* MBEDTLS_LIBRARY_COMMON_H */

library/ssl_tls13_keys.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels =
8282
* the HkdfLabel structure on success.
8383
*/
8484

85-
static const char tls13_label_prefix[6] = "tls13 ";
85+
/* We need to tell the compiler that we meant to leave out the null character. */
86+
static const char tls13_label_prefix[6] MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING = "tls13 ";
8687

8788
#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
8889
(2 /* expansion length */ \

library/ssl_tls13_keys.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040

4141
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
4242

43+
/* We need to tell the compiler that we meant to leave out the null character. */
4344
#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
44-
const unsigned char name [sizeof(string) - 1];
45+
const unsigned char name [sizeof(string) - 1] MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING;
4546

4647
union mbedtls_ssl_tls13_labels_union {
4748
MBEDTLS_SSL_TLS1_3_LABEL_LIST

tests/suites/test_suite_psa_crypto.function

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,7 +3480,9 @@ void mac_setup(int key_type_arg,
34803480
psa_mac_operation_t operation = psa_mac_operation_init_short();
34813481
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
34823482
#if defined(KNOWN_SUPPORTED_MAC_ALG)
3483-
const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3483+
/* We need to tell the compiler that we meant to leave out the null character. */
3484+
const uint8_t smoke_test_key_data[16] MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING =
3485+
"kkkkkkkkkkkkkkkk";
34843486
#endif
34853487

34863488
PSA_ASSERT(psa_crypto_init());
@@ -3917,7 +3919,9 @@ void cipher_setup(int key_type_arg,
39173919
psa_cipher_operation_t operation = psa_cipher_operation_init_short();
39183920
psa_status_t status;
39193921
#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3920-
const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3922+
/* We need to tell the compiler that we meant to leave out the null character. */
3923+
const uint8_t smoke_test_key_data[16] MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING =
3924+
"kkkkkkkkkkkkkkkk";
39213925
#endif
39223926

39233927
PSA_ASSERT(psa_crypto_init());

tests/suites/test_suite_psa_crypto_slot_management.function

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,9 @@ void create_existent(int lifetime_arg, int owner_id_arg, int id_arg,
377377
mbedtls_svc_key_id_t returned_id = MBEDTLS_SVC_KEY_ID_INIT;
378378
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
379379
psa_key_type_t type1 = PSA_KEY_TYPE_RAW_DATA;
380-
const uint8_t material1[5] = "a key";
381-
const uint8_t material2[5] = "b key";
380+
/* We need to tell the compiler that we meant to leave out the null character. */
381+
const uint8_t material1[5] MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING = "a key";
382+
const uint8_t material2[5] MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING = "b key";
382383
size_t bits1 = PSA_BYTES_TO_BITS(sizeof(material1));
383384
uint8_t reexported[sizeof(material1)];
384385
size_t reexported_length;
@@ -747,7 +748,7 @@ void invalid_handle(int handle_construction,
747748
psa_key_id_t key_id;
748749
psa_status_t close_status = close_status_arg;
749750
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
750-
uint8_t material[1] = "a";
751+
uint8_t material[1] = { 'a' };
751752

752753
PSA_ASSERT(psa_crypto_init());
753754

tests/suites/test_suite_ssl_decrypt.function

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ void ssl_decrypt_null(int hash_id)
3737
mbedtls_ssl_write_version(rec_good.ver,
3838
MBEDTLS_SSL_TRANSPORT_STREAM,
3939
version);
40-
const char sample_plaintext[3] = "ABC";
40+
/* We need to tell the compiler that we meant to leave out the null character. */
41+
const char sample_plaintext[3] MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING = "ABC";
4142
mbedtls_ssl_context ssl;
4243
mbedtls_ssl_init(&ssl);
4344
uint8_t *buf = NULL;

0 commit comments

Comments
 (0)