]> git.itanic.dy.fi Git - linux-stable/commitdiff
mlxsw: spectrum_ethtool: Fix -Wformat-truncation warning
authorIdo Schimmel <idosch@nvidia.com>
Fri, 6 Oct 2023 14:43:17 +0000 (16:43 +0200)
committerPaolo Abeni <pabeni@redhat.com>
Tue, 10 Oct 2023 10:15:50 +0000 (12:15 +0200)
Ethtool stats strings cannot be longer than 32 characters
('ETH_GSTRING_LEN'), including the terminating null byte. The format
string '%.29s_%.1d' can exceed this limitation if the per-TC counter
name exceeds 28 characters. Together with the underscore, the two digits
of the TC (bounded at 16) and the terminating null byte, more than 32
characters will be used.

Fix this by bounding the counter name at 28 characters which suppresses
the following build warning [1]. This does not affect ethtool output
since the longest counter name does not exceed this limitation.

[1]
drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c: In function ‘mlxsw_sp_port_get_strings’:
drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c:622:58: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=]
  622 |                 snprintf(*p, ETH_GSTRING_LEN, "%.29s_%.1d",
      |                                                          ^
In function ‘mlxsw_sp_port_get_tc_strings’,
    inlined from ‘mlxsw_sp_port_get_strings’ at drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c:677:4:
drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c:622:17: note: ‘snprintf’ output between 3 and 33 bytes into a destination of size 32
  622 |                 snprintf(*p, ETH_GSTRING_LEN, "%.29s_%.1d",
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  623 |                          mlxsw_sp_port_hw_tc_stats[i].str, tc);
      |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c: In function ‘mlxsw_sp_port_get_strings’:
drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c:622:58: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=]
  622 |                 snprintf(*p, ETH_GSTRING_LEN, "%.29s_%.1d",
      |                                                          ^
In function ‘mlxsw_sp_port_get_tc_strings’,
    inlined from ‘mlxsw_sp_port_get_strings’ at drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c:677:4:
drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c:622:17: note: ‘snprintf’ output between 3 and 33 bytes into a destination of size 32
  622 |                 snprintf(*p, ETH_GSTRING_LEN, "%.29s_%.1d",
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  623 |                          mlxsw_sp_port_hw_tc_stats[i].str, tc);
      |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Ido Schimmel <idosch@nvidia.com>
Reviewed-by: Petr Machata <petrm@nvidia.com>
Signed-off-by: Petr Machata <petrm@nvidia.com>
Reviewed-by: Simon Horman <horms@kernel.org>
Tested-by: Simon Horman <horms@kernel.org> # build-tested
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
drivers/net/ethernet/mellanox/mlxsw/spectrum_ethtool.c

index 472830d07ac12e35012d311e3fa9a1cb1584fdd3..0f29e9c194113900afe55eaf6a17bd65b414f9e9 100644 (file)
@@ -619,7 +619,7 @@ static void mlxsw_sp_port_get_tc_strings(u8 **p, int tc)
        int i;
 
        for (i = 0; i < MLXSW_SP_PORT_HW_TC_STATS_LEN; i++) {
-               snprintf(*p, ETH_GSTRING_LEN, "%.29s_%.1d",
+               snprintf(*p, ETH_GSTRING_LEN, "%.28s_%d",
                         mlxsw_sp_port_hw_tc_stats[i].str, tc);
                *p += ETH_GSTRING_LEN;
        }