Skip to contentSkip to conversation

platform/mellanox: mlxbf-pmc: fix sscanf() error checking

PATCH In review Dan Carpenter 3 participants 3 messages May 15, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/mellanox: mlxbf-pmc: fix sscanf() error checking

The sscanf() function never returns negatives. It returns the number of items successfully read.

Fixes 1a218d312e65 ("platform/mellanox: mlxbf-pmc: Add Mellanox BlueField PMC driver")
---
 drivers/platform/mellanox/mlxbf-pmc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/platform/mellanox/mlxbf-pmc.c +2 -3
index c2c9b0d3244c..be967d797c28 100644
@@ -1348,9 +1348,8 @@ static int mlxbf_pmc_map_counters(struct device *dev)
 
 	for (i = 0; i < pmc->total_blocks; ++i) {
 		if (strstr(pmc->block_name[i], "tile")) {
-			ret = sscanf(pmc->block_name[i], "tile%d", &tile_num);
-			if (ret < 0)
-				return ret;
+			if (sscanf(pmc->block_name[i], "tile%d", &tile_num) != 1)
+				return -EINVAL;
 
 			if (tile_num >= pmc->tile_count)
 				continue;
Signature · 1 line
2.39.2
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Dan Carpenter

The sscanf() function never returns negatives. It returns the number of items successfully read.

Fixes 1a218d312e65 ("platform/mellanox: mlxbf-pmc: Add Mellanox BlueField PMC driver")
---
 drivers/platform/mellanox/mlxbf-pmc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/platform/mellanox/mlxbf-pmc.c +0 -0
index c2c9b0d3244c..be967d797c28 100644
@@ -1348,9 +1348,8 @@ static int mlxbf_pmc_map_counters(struct device *dev)
 

drivers/platform/mellanox/mlxbf-pmc.c static int mlxbf_pmc_map_counters(struct device *dev) · line 1355

Quoted 10 lines
drivers/platform/mellanox/mlxbf-pmc.c +2 -3
 	for (i = 0; i < pmc->total_blocks; ++i) {
 		if (strstr(pmc->block_name[i], "tile")) {
-			ret = sscanf(pmc->block_name[i], "tile%d", &tile_num);
-			if (ret < 0)
-				return ret;
+			if (sscanf(pmc->block_name[i], "tile%d", &tile_num) != 1)
+				return -EINVAL;
 
 			if (tile_num >= pmc->tile_count)
 				continue;
Signature · 1 line
 i.
#2 lore raw

Hi,

Quoted 25 lines · Ilpo Järvinen
Quoted 23 lines · Dan Carpenter

The sscanf() function never returns negatives. It returns the number of items successfully read.

Fixes 1a218d312e65 ("platform/mellanox: mlxbf-pmc: Add Mellanox BlueField PMC driver")
---
 drivers/platform/mellanox/mlxbf-pmc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/platform/mellanox/mlxbf-pmc.c +2 -3
index c2c9b0d3244c..be967d797c28 100644
@@ -1348,9 +1348,8 @@ static int mlxbf_pmc_map_counters(struct device *dev)
 
 	for (i = 0; i < pmc->total_blocks; ++i) {
 		if (strstr(pmc->block_name[i], "tile")) {
-			ret = sscanf(pmc->block_name[i], "tile%d", &tile_num);
-			if (ret < 0)
-				return ret;
+			if (sscanf(pmc->block_name[i], "tile%d", &tile_num) != 1)
+				return -EINVAL;
 
Quoted 4 lines

drivers/platform/mellanox/mlxbf-pmc.c static int mlxbf_pmc_map_counters(struct device *dev) · line 1355

Quoted 2 lines
drivers/platform/mellanox/mlxbf-pmc.c +0 -0
 			if (tile_num >= pmc->tile_count)
 				continue;

Thank you for your patch, I've applied this patch to my fixes branch:
https://git.kernel.org/pub/scm/linux/kernel/git/pdx86/platform-drivers-x86.git/log/?h=fixes

Note it will show up in my fixes branch once I've pushed my local branch there, which might take a while.

I will include this patch in my next fixes pull-req to Linus for the current kernel development cycle.

Regards,

Hans