Skip to content

Commit 42707db

Browse files
committed
patches: Add series addressing -Wunterminated-string-initialization
This is enabled in -Wextra, which is on by default in 6.10 and newer. Signed-off-by: Nathan Chancellor <[email protected]>
1 parent e1afa72 commit 42707db

10 files changed

+826
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
From d5d45a7f26194460964eb5677a9226697f7b7fdd Mon Sep 17 00:00:00 2001
2+
From: Linus Torvalds <[email protected]>
3+
Date: Sun, 20 Apr 2025 10:33:23 -0700
4+
Subject: gcc-15: make 'unterminated string initialization' just a warning
5+
MIME-Version: 1.0
6+
Content-Type: text/plain; charset=UTF-8
7+
Content-Transfer-Encoding: 8bit
8+
9+
gcc-15 enabling -Wunterminated-string-initialization in -Wextra by
10+
default was done with the best intentions, but the warning is still
11+
quite broken.
12+
13+
What annoys me about the warning is that this is a very traditional AND
14+
CORRECT way to initialize fixed byte arrays in C:
15+
16+
unsigned char hex[16] = "0123456789abcdef";
17+
18+
and we use this all over the kernel. And the warning is fine, but gcc
19+
developers apparently never made a reasonable way to disable it. As is
20+
(sadly) tradition with these things.
21+
22+
Yes, there's "__attribute__((nonstring))", and we have a macro to make
23+
that absolutely disgusting syntax more palatable (ie the kernel syntax
24+
for that monstrosity is just "__nonstring").
25+
26+
But that attribute is misdesigned. What you'd typically want to do is
27+
tell the compiler that you are using a type that isn't a string but a
28+
byte array, but that doesn't work at all:
29+
30+
warning: ‘nonstring’ attribute does not apply to types [-Wattributes]
31+
32+
and because of this fundamental mis-design, you then have to mark each
33+
instance of that pattern.
34+
35+
This is particularly noticeable in our ACPI code, because ACPI has this
36+
notion of a 4-byte "type name" that gets used all over, and is exactly
37+
this kind of byte array.
38+
39+
This is a sad oversight, because the warning is useful, but really would
40+
be so much better if gcc had also given a sane way to indicate that we
41+
really just want a byte array type at a type level, not the broken "each
42+
and every array definition" level.
43+
44+
So now instead of creating a nice "ACPI name" type using something like
45+
46+
typedef char acpi_name_t[4] __nonstring;
47+
48+
we have to do things like
49+
50+
char name[ACPI_NAMESEG_SIZE] __nonstring;
51+
52+
in every place that uses this concept and then happens to have the
53+
typical initializers.
54+
55+
This is annoying me mainly because I think the warning _is_ a good
56+
warning, which is why I'm not just turning it off in disgust. But it is
57+
hampered by this bad implementation detail.
58+
59+
[ And obviously I'm doing this now because system upgrades for me are
60+
something that happen in the middle of the release cycle: don't do it
61+
before or during travel, or just before or during the busy merge
62+
window period. ]
63+
64+
Signed-off-by: Linus Torvalds <[email protected]>
65+
---
66+
Link: https://git.kernel.org/linus/d5d45a7f26194460964eb5677a9226697f7b7fdd
67+
---
68+
Makefile | 3 +++
69+
1 file changed, 3 insertions(+)
70+
71+
diff --git a/Makefile b/Makefile
72+
index e65f8735c7bf64..0a9992db4fe026 100644
73+
--- a/Makefile
74+
+++ b/Makefile
75+
@@ -1056,6 +1056,9 @@ KBUILD_CFLAGS += $(call cc-option, -fstrict-flex-arrays=3)
76+
KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(call cc-option, -Wno-stringop-overflow)
77+
KBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(call cc-option, -Wstringop-overflow)
78+
79+
+#Currently, disable -Wunterminated-string-initialization as an error
80+
+KBUILD_CFLAGS += $(call cc-option, -Wno-error=unterminated-string-initialization)
81+
+
82+
# disable invalid "can't wrap" optimizations for signed / pointers
83+
KBUILD_CFLAGS += -fno-strict-overflow
84+
85+
--
86+
cgit 1.2.3-korg
87+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
From 9d7a0577c9db35c4cc52db90bc415ea248446472 Mon Sep 17 00:00:00 2001
2+
From: Linus Torvalds <[email protected]>
3+
Date: Sun, 20 Apr 2025 15:30:53 -0700
4+
Subject: gcc-15: disable '-Wunterminated-string-initialization' entirely for
5+
now
6+
7+
I had left the warning around but as a non-fatal error to get my gcc-15
8+
builds going, but fixed up some of the most annoying warning cases so
9+
that it wouldn't be *too* verbose.
10+
11+
Because I like the _concept_ of the warning, even if I detested the
12+
implementation to shut it up.
13+
14+
It turns out the implementation to shut it up is even more broken than I
15+
thought, and my "shut up most of the warnings" patch just caused fatal
16+
errors on gcc-14 instead.
17+
18+
I had tested with clang, but when I upgrade my development environment,
19+
I try to do it on all machines because I hate having different systems
20+
to maintain, and hadn't realized that gcc-14 now had issues.
21+
22+
The ACPI case is literally why I wanted to have a *type* that doesn't
23+
trigger the warning (see commit d5d45a7f2619: "gcc-15: make
24+
'unterminated string initialization' just a warning"), instead of
25+
marking individual places as "__nonstring".
26+
27+
But gcc-14 doesn't like that __nonstring location that shut gcc-15 up,
28+
because it's on an array of char arrays, not on one single array:
29+
30+
drivers/acpi/tables.c:399:1: error: 'nonstring' attribute ignored on objects of type 'const char[][4]' [-Werror=attributes]
31+
399 | static const char table_sigs[][ACPI_NAMESEG_SIZE] __initconst __nonstring = {
32+
| ^~~~~~
33+
34+
and my attempts to nest it properly with a type had failed, because of
35+
how gcc doesn't like marking the types as having attributes, only
36+
symbols.
37+
38+
There may be some trick to it, but I was already annoyed by the bad
39+
attribute design, now I'm just entirely fed up with it.
40+
41+
I wish gcc had a proper way to say "this type is a *byte* array, not a
42+
string".
43+
44+
The obvious thing would be to distinguish between "char []" and an
45+
explicitly signed "unsigned char []" (as opposed to an implicitly
46+
unsigned char, which is typically an architecture-specific default, but
47+
for the kernel is universal thanks to '-funsigned-char').
48+
49+
But any "we can typedef a 8-bit type to not become a string just because
50+
it's an array" model would be fine.
51+
52+
But "__attribute__((nonstring))" is sadly not that sane model.
53+
54+
Reported-by: Chris Clayton <[email protected]>
55+
Fixes: 4b4bd8c50f48 ("gcc-15: acpi: sprinkle random '__nonstring' crumbles around")
56+
Fixes: d5d45a7f2619 ("gcc-15: make 'unterminated string initialization' just a warning")
57+
Signed-off-by: Linus Torvalds <[email protected]>
58+
---
59+
Link: https://git.kernel.org/linus/9d7a0577c9db35c4cc52db90bc415ea248446472
60+
---
61+
Makefile | 4 ++--
62+
1 file changed, 2 insertions(+), 2 deletions(-)
63+
64+
diff --git a/Makefile b/Makefile
65+
index d9ead7182af9..5f1754a87c13 100644
66+
--- a/Makefile
67+
+++ b/Makefile
68+
@@ -1001,8 +1001,8 @@ KBUILD_CFLAGS += $(call cc-option, -fstrict-flex-arrays=3)
69+
KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(call cc-option, -Wno-stringop-overflow)
70+
KBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(call cc-option, -Wstringop-overflow)
71+
72+
-#Currently, disable -Wunterminated-string-initialization as an error
73+
-KBUILD_CFLAGS += $(call cc-option, -Wno-error=unterminated-string-initialization)
74+
+#Currently, disable -Wunterminated-string-initialization as broken
75+
+KBUILD_CFLAGS += $(call cc-option, -Wno-unterminated-string-initialization)
76+
77+
# disable invalid "can't wrap" optimizations for signed / pointers
78+
KBUILD_CFLAGS += -fno-strict-overflow
79+
--
80+
cgit 1.2.3-korg
81+
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
From a79be02bba5c31f967885c7f3bf3a756d77d11d9 Mon Sep 17 00:00:00 2001
2+
From: Linus Torvalds <[email protected]>
3+
Date: Wed, 23 Apr 2025 10:08:29 -0700
4+
Subject: Fix mis-uses of 'cc-option' for warning disablement
5+
MIME-Version: 1.0
6+
Content-Type: text/plain; charset=UTF-8
7+
Content-Transfer-Encoding: 8bit
8+
9+
This was triggered by one of my mis-uses causing odd build warnings on
10+
sparc in linux-next, but while figuring out why the "obviously correct"
11+
use of cc-option caused such odd breakage, I found eight other cases of
12+
the same thing in the tree.
13+
14+
The root cause is that 'cc-option' doesn't work for checking negative
15+
warning options (ie things like '-Wno-stringop-overflow') because gcc
16+
will silently accept options it doesn't recognize, and so 'cc-option'
17+
ends up thinking they are perfectly fine.
18+
19+
And it all works, until you have a situation where _another_ warning is
20+
emitted. At that point the compiler will go "Hmm, maybe the user
21+
intended to disable this warning but used that wrong option that I
22+
didn't recognize", and generate a warning for the unrecognized negative
23+
option.
24+
25+
Which explains why we have several cases of this in the tree: the
26+
'cc-option' test really doesn't work for this situation, but most of the
27+
time it simply doesn't matter that ity doesn't work.
28+
29+
The reason my recently added case caused problems on sparc was pointed
30+
out by Thomas Weißschuh: the sparc build had a previous explicit warning
31+
that then triggered the new one.
32+
33+
I think the best fix for this would be to make 'cc-option' a bit smarter
34+
about this sitation, possibly by adding an intentional warning to the
35+
test case that then triggers the unrecognized option warning reliably.
36+
37+
But the short-term fix is to replace 'cc-option' with an existing helper
38+
designed for this exact case: 'cc-disable-warning', which picks the
39+
negative warning but uses the positive form for testing the compiler
40+
support.
41+
42+
Reported-by: Stephen Rothwell <[email protected]>
43+
Link: https://lore.kernel.org/all/[email protected]/
44+
Explained-by: Thomas Weißschuh <[email protected]>
45+
Signed-off-by: Linus Torvalds <[email protected]>
46+
---
47+
Link: https://git.kernel.org/linus/a79be02bba5c31f967885c7f3bf3a756d77d11d9
48+
---
49+
Makefile | 4 ++--
50+
arch/loongarch/kernel/Makefile | 8 ++++----
51+
arch/loongarch/kvm/Makefile | 2 +-
52+
arch/riscv/kernel/Makefile | 4 ++--
53+
scripts/Makefile.extrawarn | 2 +-
54+
5 files changed, 10 insertions(+), 10 deletions(-)
55+
56+
diff --git a/Makefile b/Makefile
57+
index 5f1754a87c13..525083444cb9 100644
58+
--- a/Makefile
59+
+++ b/Makefile
60+
@@ -998,11 +998,11 @@ NOSTDINC_FLAGS += -nostdinc
61+
KBUILD_CFLAGS += $(call cc-option, -fstrict-flex-arrays=3)
62+
63+
#Currently, disable -Wstringop-overflow for GCC 11, globally.
64+
-KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(call cc-option, -Wno-stringop-overflow)
65+
+KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(call cc-disable-warning, stringop-overflow)
66+
KBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(call cc-option, -Wstringop-overflow)
67+
68+
#Currently, disable -Wunterminated-string-initialization as broken
69+
-KBUILD_CFLAGS += $(call cc-option, -Wno-unterminated-string-initialization)
70+
+KBUILD_CFLAGS += $(call cc-disable-warning, unterminated-string-initialization)
71+
72+
# disable invalid "can't wrap" optimizations for signed / pointers
73+
KBUILD_CFLAGS += -fno-strict-overflow
74+
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
75+
index c9bfeda89e40..66132683f1ed 100644
76+
--- a/arch/loongarch/kernel/Makefile
77+
+++ b/arch/loongarch/kernel/Makefile
78+
@@ -21,10 +21,10 @@ obj-$(CONFIG_CPU_HAS_LBT) += lbt.o
79+
80+
obj-$(CONFIG_ARCH_STRICT_ALIGN) += unaligned.o
81+
82+
-CFLAGS_module.o += $(call cc-option,-Wno-override-init,)
83+
-CFLAGS_syscall.o += $(call cc-option,-Wno-override-init,)
84+
-CFLAGS_traps.o += $(call cc-option,-Wno-override-init,)
85+
-CFLAGS_perf_event.o += $(call cc-option,-Wno-override-init,)
86+
+CFLAGS_module.o += $(call cc-disable-warning, override-init)
87+
+CFLAGS_syscall.o += $(call cc-disable-warning, override-init)
88+
+CFLAGS_traps.o += $(call cc-disable-warning, override-init)
89+
+CFLAGS_perf_event.o += $(call cc-disable-warning, override-init)
90+
91+
ifdef CONFIG_FUNCTION_TRACER
92+
ifndef CONFIG_DYNAMIC_FTRACE
93+
diff --git a/arch/loongarch/kvm/Makefile b/arch/loongarch/kvm/Makefile
94+
index b2f4cbe01ae8..2e188e8f1468 100644
95+
--- a/arch/loongarch/kvm/Makefile
96+
+++ b/arch/loongarch/kvm/Makefile
97+
@@ -19,4 +19,4 @@ kvm-y += tlb.o
98+
kvm-y += vcpu.o
99+
kvm-y += vm.o
100+
101+
-CFLAGS_exit.o += $(call cc-option,-Wno-override-init,)
102+
+CFLAGS_exit.o += $(call cc-disable-warning, override-init)
103+
diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
104+
index 69dc8aaab3fb..6b3b5255c889 100644
105+
--- a/arch/riscv/kernel/Makefile
106+
+++ b/arch/riscv/kernel/Makefile
107+
@@ -9,8 +9,8 @@ CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE)
108+
CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE)
109+
CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE)
110+
endif
111+
-CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,)
112+
-CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,)
113+
+CFLAGS_syscall_table.o += $(call cc-disable-warning, override-init)
114+
+CFLAGS_compat_syscall_table.o += $(call cc-disable-warning, override-init)
115+
116+
ifdef CONFIG_KEXEC_CORE
117+
AFLAGS_kexec_relocate.o := -mcmodel=medany $(call cc-option,-mno-relax)
118+
diff --git a/scripts/Makefile.extrawarn b/scripts/Makefile.extrawarn
119+
index dc081cf46d21..c38067c1d655 100644
120+
--- a/scripts/Makefile.extrawarn
121+
+++ b/scripts/Makefile.extrawarn
122+
@@ -15,7 +15,7 @@ KBUILD_CFLAGS += -Werror=return-type
123+
KBUILD_CFLAGS += -Werror=strict-prototypes
124+
KBUILD_CFLAGS += -Wno-format-security
125+
KBUILD_CFLAGS += -Wno-trigraphs
126+
-KBUILD_CFLAGS += $(call cc-disable-warning,frame-address,)
127+
+KBUILD_CFLAGS += $(call cc-disable-warning, frame-address)
128+
KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
129+
KBUILD_CFLAGS += -Wmissing-declarations
130+
KBUILD_CFLAGS += -Wmissing-prototypes
131+
--
132+
cgit 1.2.3-korg
133+

0 commit comments

Comments
 (0)