Skip to content

Commit b688f36

Browse files
committed
compiler_types: Introduce __nonstring_array
GCC has expanded support of the "nonstring" attribute so that it can be applied to arrays of character arrays[1], which is needed to identify correct static initialization of those kinds of objects. Since this was not supported prior to GCC 15, we need to distinguish the usage of Linux's existing __nonstring macro for the attribute for non-multi-dimensional char arrays. Until GCC 15 is the minimum version, use __nonstring_array to mark arrays of non-string character arrays. (Regular non-string character arrays can continue to use __nonstring.) Once GCC 15 is the minimum compiler version we can replace all uses of __nonstring_array with just __nonstring and remove this macro. This allows for changes like this: -static const char table_sigs[][ACPI_NAMESEG_SIZE] __initconst = { +static const char table_sigs[][ACPI_NAMESEG_SIZE] __nonstring_array __initconst = { ACPI_SIG_BERT, ACPI_SIG_BGRT, ACPI_SIG_CPEP, ACPI_SIG_ECDT, Which will silence the coming -Wunterminated-string-initialization warnings in GCC 15: In file included from ../include/acpi/actbl.h:371, from ../include/acpi/acpi.h:26, from ../include/linux/acpi.h:26, from ../drivers/acpi/tables.c:19: ../include/acpi/actbl1.h:30:33: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (5 chars into 4 available) [-Wunterminated-string-initialization] 30 | #define ACPI_SIG_BERT "BERT" /* Boot Error Record Table */ | ^~~~~~ ../drivers/acpi/tables.c:400:9: note: in expansion of macro 'ACPI_SIG_BERT' 400 | ACPI_SIG_BERT, ACPI_SIG_BGRT, ACPI_SIG_CPEP, ACPI_SIG_ECDT, | ^~~~~~~~~~~~~ ../include/acpi/actbl1.h:31:33: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (5 chars into 4 available) [-Wunterminated-string-initialization] 31 | #define ACPI_SIG_BGRT "BGRT" /* Boot Graphics Resource Table */ | ^~~~~~ Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117178 [1] Acked-by: Miguel Ojeda <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent d70da12 commit b688f36

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

include/linux/compiler_types.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,18 @@ struct ftrace_likely_data {
348348
# define __counted_by(member)
349349
#endif
350350

351+
/*
352+
* Optional: only supported since gcc >= 15
353+
* Optional: not supported by Clang
354+
*
355+
* gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117178
356+
*/
357+
#ifdef CONFIG_CC_HAS_MULTIDIMENSIONAL_NONSTRING
358+
# define __nonstring_array __attribute__((__nonstring__))
359+
#else
360+
# define __nonstring_array
361+
#endif
362+
351363
/*
352364
* Apply __counted_by() when the Endianness matches to increase test coverage.
353365
*/

init/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ config CC_HAS_COUNTED_BY
129129
# https://github.com/llvm/llvm-project/pull/112636
130130
depends on !(CC_IS_CLANG && CLANG_VERSION < 190103)
131131

132+
config CC_HAS_MULTIDIMENSIONAL_NONSTRING
133+
def_bool $(success,echo 'char tag[][4] __attribute__((__nonstring__)) = { };' | $(CC) $(CLANG_FLAGS) -x c - -c -o /dev/null -Werror)
134+
132135
config RUSTC_HAS_COERCE_POINTEE
133136
def_bool RUSTC_VERSION >= 108400
134137

0 commit comments

Comments
 (0)