Skip to contentSkip to conversation

net: macb: Add __nonstring annotations for unterminated strings

PATCH Superseded Kees Cook 3 participants 4 messages Mar 11, 2025 – Mar 18, 2025

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v2 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 and correctly identify the char array as "not a C string" and thereby eliminate the warning.

---
v1: https://lore.kernel.org/lkml/[email protected]/
v2: switch to __nonstring annotation
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 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/ethernet/cadence/macb.h +2 -1
index 2847278d9cd4..003483073223 100644
@@ -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;
 };
@@ -1068,6 +1068,7 @@ static const struct gem_statistic gem_statistics[] = {
 	GEM_STAT_TITLE(TX512CNT, "tx_512_1023_byte_frames"),
 	GEM_STAT_TITLE(TX1024CNT, "tx_1024_1518_byte_frames"),
 	GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),
+
 	GEM_STAT_TITLE_BITS(TXURUNCNT, "tx_underrun",
 			    GEM_BIT(NDS_TXERR)|GEM_BIT(NDS_TXFIFOERR)),
 	GEM_STAT_TITLE_BITS(SNGLCOLLCNT, "tx_single_collision_frames",
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 5 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 __nonstring to and correctly identify the char array as "not a C

s/__nonstring to and correctly/__nonstring to correctly/ ?

Quoted 33 lines

string" and thereby eliminate the warning.

---
v1: https://lore.kernel.org/lkml/[email protected]/
v2: switch to __nonstring annotation
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 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/ethernet/cadence/macb.h +1 -1
index 2847278d9cd4..003483073223 100644
@@ -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 static const struct gem_statistic gem_statistics[] = { · line 1071

Quoted 4 lines
drivers/net/ethernet/cadence/macb.h +1 -0
@@ -1068,6 +1068,7 @@ static const struct gem_statistic gem_statistics[] = {
        GEM_STAT_TITLE(TX512CNT, "tx_512_1023_byte_frames"),
        GEM_STAT_TITLE(TX1024CNT, "tx_1024_1518_byte_frames"),
        GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),
+

Is this an errant inclusion? :-)

Quoted 7 lines
drivers/net/ethernet/cadence/macb.h +0 -1
        GEM_STAT_TITLE_BITS(TXURUNCNT, "tx_underrun",
                            GEM_BIT(NDS_TXERR)|GEM_BIT(NDS_TXFIFOERR)),
        GEM_STAT_TITLE_BITS(SNGLCOLLCNT, "tx_single_collision_frames",
--

2.34.1

author
#2 lore raw
Quoted 8 lines · Bill Wendling
Quoted 5 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 __nonstring to and correctly identify the char array as "not a C

s/__nonstring to and correctly/__nonstring to correctly/ ?

Whoops. Rewriting glitch. Yes, "and" should be dropped. I will fix this locally for all the patches with the same language and send v3 for this one.

Quoted 32 lines
Quoted 32 lines

string" and thereby eliminate the warning.

---
v1: https://lore.kernel.org/lkml/[email protected]/
v2: switch to __nonstring annotation
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 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/ethernet/cadence/macb.h +1 -1
index 2847278d9cd4..003483073223 100644
@@ -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;
Quoted 8 lines

drivers/net/ethernet/cadence/macb.h static const struct gem_statistic gem_statistics[] = { · line 1071

Quoted 6 lines
drivers/net/ethernet/cadence/macb.h +1 -0
 };
@@ -1068,6 +1068,7 @@ static const struct gem_statistic gem_statistics[] = {
        GEM_STAT_TITLE(TX512CNT, "tx_512_1023_byte_frames"),
        GEM_STAT_TITLE(TX1024CNT, "tx_1024_1518_byte_frames"),
        GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),
+

Is this an errant inclusion? :-)

Ah, yeah, I had removed the truncation change but the whitespace change stuck around. Also fixing!

Quoted 2 lines

Thanks!

-Kees

Signature · 1 line
Kees Cook
reviewer
#3 lore raw
In reply to Kees Cook
Quoted 28 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 __nonstring to and correctly identify the char array as "not a C string" and thereby eliminate the warning.

---
v1: https://lore.kernel.org/lkml/[email protected]/
v2: switch to __nonstring annotation
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 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/ethernet/cadence/macb.h +0 -0
index 2847278d9cd4..003483073223 100644

drivers/net/ethernet/cadence/macb.h struct gem_stats { · line 1030

Quoted 5 lines
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;

Hi Kees, all,

I see where this is going, and I have no objections to it. But a member called *_string which is annotated as __nonstring does seem a somewhat unintuitive.

Quoted 15 lines
drivers/net/ethernet/cadence/macb.h +1 -0
 	int offset;
 	u32 stat_bits;
 };
@@ -1068,6 +1068,7 @@ static const struct gem_statistic gem_statistics[] = {
 	GEM_STAT_TITLE(TX512CNT, "tx_512_1023_byte_frames"),
 	GEM_STAT_TITLE(TX1024CNT, "tx_1024_1518_byte_frames"),
 	GEM_STAT_TITLE(TX1519CNT, "tx_greater_than_1518_byte_frames"),
+
 	GEM_STAT_TITLE_BITS(TXURUNCNT, "tx_underrun",
 			    GEM_BIT(NDS_TXERR)|GEM_BIT(NDS_TXFIFOERR)),
 	GEM_STAT_TITLE_BITS(SNGLCOLLCNT, "tx_single_collision_frames",
Signature · 1 line
2.34.1