net: macb: Add __nonstring annotations for unterminated strings
Subsystems (3)
PATCH v3 1/1 net: macb: Add __nonstring annotations for unterminated strings
When a character array without a terminating NUL character has a static initializer, GCC 15's -Wunterminated-string-initialization will only warn if the array lacks the "nonstring" attribute[1]. Mark the arrays with __nonstring to correctly identify the char array as "not a C string" and thereby eliminate the warning:
In file included from ../drivers/net/ethernet/cadence/macb_main.c:42:../drivers/net/ethernet/cadence/macb.h:1070:35: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (33 chars into 32 available) [-Wunterminated-string-initialization]
1070 | GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../drivers/net/ethernet/cadence/macb.h:1050:24: note: in definition of macro 'GEM_STAT_TITLE_BITS'
1050 | .stat_string = title, \
| ^~~~~../drivers/net/ethernet/cadence/macb.h:1070:9: note: in expansion of macro 'GEM_STAT_TITLE'
1070 | GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),
| ^~~~~~~~~~~~~~
../drivers/net/ethernet/cadence/macb.h:1097:35: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (33 chars into 32 available) [-Wunterminated-string-initialization]
1097 | GEM_STAT_TITLE(RX1519CNT, "rx_greater_than_1518_byte_frames"),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~../drivers/net/ethernet/cadence/macb.h:1050:24: note: in definition of macro 'GEM_STAT_TITLE_BITS'
1050 | .stat_string = title, \
| ^~~~~../drivers/net/ethernet/cadence/macb.h:1097:9: note: in expansion of macro 'GEM_STAT_TITLE'
1097 | GEM_STAT_TITLE(RX1519CNT, "rx_greater_than_1518_byte_frames"),
| ^~~~~~~~~~~~~~Since these strings are copied with memcpy() they do not need to be NUL terminated, and can use __nonstring:
memcpy(p, gem_statistics[i].stat_string,
ETH_GSTRING_LEN);---
v3: improve commit message (Bill), drop whitespace change (Bill)
v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
Cc: Nicolas Ferre <[email protected]>
Cc: Claudiu Beznea <[email protected]>
Cc: Andrew Lunn <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: [email protected]
--- drivers/net/ethernet/cadence/macb.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/cadence/macb.h +1 -1
@@ -1027,7 +1027,7 @@ struct gem_stats {* this register should contribute to. */ struct gem_statistic { -char stat_string[ETH_GSTRING_LEN ]; + char stat_string[ETH_GSTRING_LEN] __nonstring;int offset;u32 stat_bits; };
Signature · 1 line
2.34.1
Quoted 29 lines · Kees Cook
When a character array without a terminating NUL character has a static initializer, GCC 15's -Wunterminated-string-initialization will only warn if the array lacks the "nonstring" attribute[1]. Mark the arrays with
__nonstringto correctly identify the char array as "not a C string" and thereby eliminate the warning:In file included from
../drivers/net/ethernet/cadence/macb_main.c:42:../drivers/net/ethernet/cadence/macb.h:1070:35: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (33 chars into 32 available) [-Wunterminated-string-initialization]
1070 |GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../drivers/net/ethernet/cadence/macb.h:1050:24: note: in definition of macro 'GEM_STAT_TITLE_BITS'
1050 | .stat_string = title, \
| ^~~~~../drivers/net/ethernet/cadence/macb.h:1070:9: note: in expansion of macro 'GEM_STAT_TITLE'1070 | GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"), | ^~~~~~~~~~~~~~ ../drivers/net/ethernet/cadence/macb.h:1097:35: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (33 chars into 32 available) [-Wunterminated-string-initialization] 1097 | GEM_STAT_TITLE(RX1519CNT, "rx_greater_than_1518_byte_frames"), | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../drivers/net/ethernet/cadence/macb.h:1050:24: note: in definition of macro 'GEM_STAT_TITLE_BITS'
1050 | .stat_string = title, \
| ^~~~~../drivers/net/ethernet/cadence/macb.h:1097:9: note: in expansion of macro 'GEM_STAT_TITLE'1097 | GEM_STAT_TITLE(RX1519CNT, "rx_greater_than_1518_byte_frames"), | ^~~~~~~~~~~~~~Since these strings are copied with
memcpy()they do not need to be NUL terminated, and can use__nonstring:
Quoted 6 lines
memcpy(p, gem_statistics[i].stat_string, ETH_GSTRING_LEN);Signed-off-by Kees Cook <[email protected]>---
Quoted 28 lines
v3: improve commit message (Bill), drop whitespace change (Bill)
v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/lkml/[email protected]/
Cc: Nicolas Ferre <[email protected]>
Cc: Claudiu Beznea <[email protected]>
Cc: Andrew Lunn <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: [email protected]--- drivers/net/ethernet/cadence/macb.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/net/ethernet/cadence/macb.h +1
-1@@ -1027,7 +1027,7 @@ struct gem_stats {* this register should contribute to. */ struct gem_statistic { -char stat_string[ETH_GSTRING_LEN ]; + char stat_string[ETH_GSTRING_LEN] __nonstring;int offset;u32 stat_bits; };
drivers/net/ethernet/cadence/macb.h struct gem_stats { · line 1030
Quoted 3 lines
struct
gem_statistic{drivers/net/ethernet/cadence/macb.h +1
-1-char stat_string[ETH_GSTRING_LEN ]; + char stat_string[ETH_GSTRING_LEN] __nonstring;
This general pattern of a char foo[ETH_GSTRING_LEN]' will appear throughout drivers/net/ethernet. Maybe Coccinelle can find them all and add the __nonstring ?
Andrew
Quoted 7 lines · Andrew Lunn
drivers/net/ethernet/cadence/macb.h struct gem_stats { · line 1030
Quoted 3 lines
struct
gem_statistic{drivers/net/ethernet/cadence/macb.h +1
-1-char stat_string[ETH_GSTRING_LEN ]; + char stat_string[ETH_GSTRING_LEN] __nonstring;This general pattern of a char foo[ETH_GSTRING_LEN]' will appear throughout
drivers/net/ethernet. Maybe Coccinelle can find them all and add the__nonstring?
I had that same thought but then rediscovered ethtool_puts() and ethtool_sprintf(), which operate on C Strings and write out C Strings... so _some_ ethtool stats are being constructed in a way that we can't just universally apply __nonstring to all ETH_GSTRING_LEN strings. :(
Signature · 1 line
Kees Cook
This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Quoted 19 lines · On Wed, 12 Mar 2025 13:07:01 -0700 you wrote:
When a character array without a terminating NUL character has a static initializer, GCC 15's -Wunterminated-string-initialization will only warn if the array lacks the "nonstring" attribute[1]. Mark the arrays with
__nonstringto correctly identify the char array as "not a C string" and thereby eliminate the warning:In file included from
../drivers/net/ethernet/cadence/macb_main.c:42:../drivers/net/ethernet/cadence/macb.h:1070:35: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (33 chars into 32 available) [-Wunterminated-string-initialization]
1070 |GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../drivers/net/ethernet/cadence/macb.h:1050:24: note: in definition of macro 'GEM_STAT_TITLE_BITS'
1050 | .stat_string = title, \
| ^~~~~../drivers/net/ethernet/cadence/macb.h:1070:9: note: in expansion of macro 'GEM_STAT_TITLE'1070 | GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"), | ^~~~~~~~~~~~~~ ../drivers/net/ethernet/cadence/macb.h:1097:35: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (33 chars into 32 available) [-Wunterminated-string-initialization] 1097 | GEM_STAT_TITLE(RX1519CNT, "rx_greater_than_1518_byte_frames"), | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 8 lines
../drivers/net/ethernet/cadence/macb.h:1050:24: note: in definition of macro 'GEM_STAT_TITLE_BITS'
1050 | .stat_string = title, \
| ^~~~~../drivers/net/ethernet/cadence/macb.h:1097:9: note: in expansion of macro 'GEM_STAT_TITLE'1097 | GEM_STAT_TITLE(RX1519CNT, "rx_greater_than_1518_byte_frames"), | ^~~~~~~~~~~~~~[...]
Here is the summary with links:
- [v3] net: macb: Add __nonstring annotations for unterminated strings
https://git.kernel.org/netdev/net-next/c/3d97da0ee625
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html