diff mbox series

[net] net/mlx5e: Fix snprintf return check

Message ID d17868ea-cef9-4f8c-a318-9f98b8341f5b@moroto.mountain (mailing list archive)
State Awaiting Upstream
Delegated to: Netdev Maintainers
Headers show
Series [net] net/mlx5e: Fix snprintf return check | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/codegen success Generated files up to date
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1115 this patch: 1115
netdev/cc_maintainers success CCed 9 of 9 maintainers
netdev/build_clang success Errors and warnings before: 1142 this patch: 1142
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 1142 this patch: 1142
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 16 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Dan Carpenter Nov. 27, 2023, 1 p.m. UTC
This code prints a string and then if there wasn't enough space for the
whole string, then it prints a slightly shorter string.  However, the
test for overflow should have been >= instead of == because snprintf()
returns the number of bytes which *would* have been printed if there
were enough space.

Fixes: 41e63c2baa11 ("net/mlx5e: Check return value of snprintf writing to fw_version buffer")
Fixes: 1b2bd0c0264f ("net/mlx5e: Check return value of snprintf writing to fw_version buffer for representors")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
 drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c | 2 +-
 drivers/net/ethernet/mellanox/mlx5/core/en_rep.c     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

Comments

Rahul Rameshbabu Nov. 27, 2023, 6:46 p.m. UTC | #1
On Mon, 27 Nov, 2023 16:00:53 +0300 Dan Carpenter <dan.carpenter@linaro.org> wrote:
> This code prints a string and then if there wasn't enough space for the
> whole string, then it prints a slightly shorter string.  However, the
> test for overflow should have been >= instead of == because snprintf()
> returns the number of bytes which *would* have been printed if there
> were enough space.
>
> Fixes: 41e63c2baa11 ("net/mlx5e: Check return value of snprintf writing to fw_version buffer")
> Fixes: 1b2bd0c0264f ("net/mlx5e: Check return value of snprintf writing to fw_version buffer for representors")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> ---

I have already sent out patches targeting net for this on the mailing
list. That said, thanks for the follow-up.

  * https://lore.kernel.org/netdev/20231121230022.89102-1-rrameshbabu@nvidia.com/
  * https://lore.kernel.org/netdev/20231121230022.89102-2-rrameshbabu@nvidia.com/

These have been reviewed by Simon Horman.

--
Thanks,

Rahul Rameshbabu
Dan Carpenter Nov. 28, 2023, 6:06 a.m. UTC | #2
On Mon, Nov 27, 2023 at 10:46:17AM -0800, Rahul Rameshbabu wrote:
> On Mon, 27 Nov, 2023 16:00:53 +0300 Dan Carpenter <dan.carpenter@linaro.org> wrote:
> > This code prints a string and then if there wasn't enough space for the
> > whole string, then it prints a slightly shorter string.  However, the
> > test for overflow should have been >= instead of == because snprintf()
> > returns the number of bytes which *would* have been printed if there
> > were enough space.
> >
> > Fixes: 41e63c2baa11 ("net/mlx5e: Check return value of snprintf writing to fw_version buffer")
> > Fixes: 1b2bd0c0264f ("net/mlx5e: Check return value of snprintf writing to fw_version buffer for representors")
> > Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> > ---
> 
> I have already sent out patches targeting net for this on the mailing
> list. That said, thanks for the follow-up.

Ah.  Good.  I hadn't seen the earlier discussion about this.  When you
said "follow-up" I worried that I had reported this earlier and
forgotten about it.

regards,
dan carpenter
diff mbox series

Patch

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c b/drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c
index 792a0ea544cd..c7c1b667b105 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c
@@ -49,7 +49,7 @@  void mlx5e_ethtool_get_drvinfo(struct mlx5e_priv *priv,
 	count = snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
 			 "%d.%d.%04d (%.16s)", fw_rev_maj(mdev),
 			 fw_rev_min(mdev), fw_rev_sub(mdev), mdev->board_id);
-	if (count == sizeof(drvinfo->fw_version))
+	if (count >= sizeof(drvinfo->fw_version))
 		snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
 			 "%d.%d.%04d", fw_rev_maj(mdev),
 			 fw_rev_min(mdev), fw_rev_sub(mdev));
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_rep.c b/drivers/net/ethernet/mellanox/mlx5/core/en_rep.c
index fe0726c7b847..a7c77a63cc29 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_rep.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_rep.c
@@ -78,7 +78,7 @@  static void mlx5e_rep_get_drvinfo(struct net_device *dev,
 	count = snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
 			 "%d.%d.%04d (%.16s)", fw_rev_maj(mdev),
 			 fw_rev_min(mdev), fw_rev_sub(mdev), mdev->board_id);
-	if (count == sizeof(drvinfo->fw_version))
+	if (count >= sizeof(drvinfo->fw_version))
 		snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
 			 "%d.%d.%04d", fw_rev_maj(mdev),
 			 fw_rev_min(mdev), fw_rev_sub(mdev));