Kees Cook <k...@kernel.org> writes:
> Many drivers populate the stats buffer using C-String based APIs (e.g. > ethtool_sprintf() and ethtool_puts()), usually when building up the > list of stats individually (i.e. with a for() loop). This, however, > requires that the source strings be populated in such a way as to have > a terminating NUL byte in the source. > > Other drivers populate the stats buffer directly using one big memcpy() > of an entire array of strings. No NUL termination is needed here, as the > bytes are being directly passed through. Yet others will build up the > stats buffer individually, but also use memcpy(). This, too, does not > need NUL termination of the source strings. > > However, there are cases where the strings that populate the > source stats strings are exactly ETH_GSTRING_LEN long, and GCC > 15's -Wunterminated-string-initialization option complains that the > trailing NUL byte has been truncated. This situation is fine only if the > driver is using the memcpy() approach. If the C-String APIs are used, > the destination string name will have its final byte truncated by the > required trailing NUL byte applied by the C-string API. > > For drivers that are already using memcpy() but have initializers that > truncate the NUL terminator, mark their source strings as __nonstring to > silence the GCC warnings. > Specifically the following warnings were investigated and addressed: > > ../drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c:328:24: warning: > initializer-string for array of 'char' truncates NUL terminator but > destination lacks 'nonstring' attribute (33 chars into 32 available) > [-Wunterminated-string-initialization] > 328 | .str = "a_mac_control_frames_transmitted", > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > ../drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c:340:24: warning: > initializer-string for array of 'char' truncates NUL terminator but > destination lacks 'nonstring' attribute (33 chars into 32 available) > [-Wunterminated-string-initialization] > 340 | .str = "a_pause_mac_ctrl_frames_received", > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c > b/drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c > index 3f64cdbabfa3..0a8fb9c842d3 100644 > --- a/drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c > +++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c > @@ -262,7 +262,7 @@ static int mlxsw_sp_port_set_pauseparam(struct net_device > *dev, > } > > struct mlxsw_sp_port_hw_stats { > - char str[ETH_GSTRING_LEN]; > + char str[ETH_GSTRING_LEN] __nonstring; > u64 (*getter)(const char *payload); > bool cells_bytes; > }; Reviewed-by: Petr Machata <pe...@nvidia.com> # for mlxsw