From: Aiden Jeffrey <[email protected]>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128108
gcc/c/ChangeLog:
* c-typeck.cc (digest_init):
gcc/testsuite/ChangeLog:
* gcc.dg/Wunterminated-string-initialization.c:
---
Don't warn `unterminated-string-initialization` for non-char arrays (i.e. don't
warn for unterminated u16 arrays).
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128108
gcc/c/ChangeLog:
* c-typeck.cc (digest_init):
gcc/testsuite/ChangeLog:
* gcc.dg/Wunterminated-string-initialization.c:
This is a forge pull request published on the gcc-patches mailing list mailing
list
as requested by aidenhjj via Sourceware Forge
<[email protected]>.
Forge discussion: https://forge.sourceware.org/gcc/gcc/pulls/256
Get it locally using:
```
git fetch forge-upstream "+refs/versioned_pull/256/*:refs/versioned_pull/256/*"
git switch -c "pr-256-v2" "refs/versioned_pull/256/2/head"
```
Or, download the patch at: https://forge.sourceware.org/gcc/gcc/pulls/256.diff
Created on: 2026-10-23 17:21:53+00:00
Latest update: 2026-10-27 09:56:59+00:00
Changes: 2 changed files, 7 additions, 2 deletions
Head revision: gcc/gcc ref refs/pull/256/head commit
ddf5984346be71b54d31f89feb98654ebbbc7dc9
Base revision: gcc/gcc ref trunk commit
24fec6e35ee0edd44c31cfcf3f13948368d429e3 r17-5573-g24fec6e35ee0ed
Merge base: 24fec6e35ee0edd44c31cfcf3f13948368d429e3
Requested Reviewers:
Changed files:
- M: gcc/c/c-typeck.cc
- M: gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c
gcc/c/c-typeck.cc | 3 ++-
gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c | 6 +++++-
2 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 2d0200858331c..8f8af8753289c 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -10065,7 +10065,8 @@ digest_init (location_t init_loc, tree decl, tree type,
tree init,
"is too long for C++ (%wu chars into %wu "
"available)", typ1, len, avail);
else if (warn_unterminated_string_initialization
- && get_attr_nonstring_decl (decl) == NULL_TREE)
+ && get_attr_nonstring_decl (decl) == NULL_TREE
+ && char_array)
warning_at (init_loc,
OPT_Wunterminated_string_initialization,
"initializer-string for array of %qT "
diff --git a/gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c
b/gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c
index 5f25f02991976..bd3a8e0d9f348 100644
--- a/gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c
+++ b/gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c
@@ -2,6 +2,8 @@
/* { dg-do compile } */
/* { dg-options "-Wunterminated-string-initialization" } */
+#include <stdint.h>
+
char a1[] = "a";
char a2[1] = "a"; /* { dg-warning "initializer-string for array of 'char'
truncates" } */
char a2nonstring[1] __attribute__((nonstring)) = "a";
@@ -18,6 +20,7 @@ struct has_str {
char tag2[4] __attribute__((nonstring));
char tag3[4] __attribute__((nonstring));
char tag4[4] __attribute__((nonstring));
+ uint16_t utf16_1[4];
int b;
};
@@ -29,5 +32,6 @@ struct has_str foo = {
.tag1 = "AAA",
.tag2 = "BBBB",
.tag3 = "CCCCC", /* { dg-warning "initializer-string for array of 'char'
is too long" } */
- .tag4 = { 'D', 'D', 'D', 'D' }
+ .tag4 = { 'D', 'D', 'D', 'D' },
+ .utf16_1 = u"AAAA"
};
Range-diff against v1:
1: ea1a708ef9966 ! 1: ddf5984346be7 c: Only warn unterm string for char array
@@ gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c: struct
has_str {
char tag3[4] __attribute__((nonstring));
char tag4[4] __attribute__((nonstring));
+ uint16_t utf16_1[4];
-+ uint16_t utf16_2[4];
int b;
};
@@ gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c: struct
has_str foo =
.tag3 = "CCCCC", /* { dg-warning "initializer-string for array
of 'char' is too long" } */
- .tag4 = { 'D', 'D', 'D', 'D' }
+ .tag4 = { 'D', 'D', 'D', 'D' },
-+ .utf16_1 = u"AAAA",
-+ .utf16_1 = u"BBBBB" /* { dg-warning "initializer-string for array of
'char' is too long" } */
++ .utf16_1 = u"AAAA"
};
--
2.56.0