Re: [PATCH v2 2/2] net: macb: fix format-truncation warning
From: Andrew Lunn
Date: Mon Feb 16 2026 - 13:36:10 EST
On Tue, Feb 17, 2026 at 01:49:50AM +0800, Sean Chang wrote:
> Use a precision specifier in snprintf to ensure the generated
> string fits within the ETH_GSTRING_LEN (32 bytes) buffer.
>
> Signed-off-by: Sean Chang <seanwascoding@xxxxxxxxx>
> ---
> v2:
> - Split the original treewide patch into subsystem-specific commits.
> - Added more detailed commit descriptions to satisfy checkpatch.
>
> drivers/net/ethernet/cadence/macb_main.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
> index 43cd013bb70e..26f9ccadd9f6 100644
> --- a/drivers/net/ethernet/cadence/macb_main.c
> +++ b/drivers/net/ethernet/cadence/macb_main.c
> @@ -3159,8 +3159,8 @@ static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
>
> for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
> for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
> - snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
> - q, queue_statistics[i].stat_string);
> + snprintf(stat_string, ETH_GSTRING_LEN, "q%u_%.19s",
> + q, queue_statistics[i].stat_string);
These strings are special, in that they are fixed length, 32 bytes
long, and not \0 terminated. There are some helpers at the end of
linux/ethtool.h for dealing with these strings. You might want to use
them.
> memcpy(p, stat_string, ETH_GSTRING_LEN);
Also, it looks like stat_string might be one byte too
short. snprintf() will \0 terminate, so you need it big enough to hold
the \0, and then this memcpy() will discard it.
I also wounder, why is just one architecture complaining about this?
Andrew