[PATCH v2 1/2] platform/mellanox: mlxbf-pmc: Replace strcmp with strncmp

Shravan Kumar Ramani posted 2 patches 3 months, 3 weeks ago
[PATCH v2 1/2] platform/mellanox: mlxbf-pmc: Replace strcmp with strncmp
Posted by Shravan Kumar Ramani 3 months, 3 weeks ago
Since the input string passed via the command line appends a newline char,
comparison using strcmp is not correct. Use the string length of the
event_list entries to match the string using strncmp instead.

Fixes: 1a218d312e65 ("platform/mellanox: mlxbf-pmc: Add Mellanox BlueField PMC driver")
Signed-off-by: Shravan Kumar Ramani <shravankr@nvidia.com>
Reviewed-by: David Thompson <davthomspson@nvidia.com>
---
 drivers/platform/mellanox/mlxbf-pmc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/platform/mellanox/mlxbf-pmc.c b/drivers/platform/mellanox/mlxbf-pmc.c
index 900069eb186e..366c0cba447f 100644
--- a/drivers/platform/mellanox/mlxbf-pmc.c
+++ b/drivers/platform/mellanox/mlxbf-pmc.c
@@ -1215,7 +1215,7 @@ static int mlxbf_pmc_get_event_num(const char *blk, const char *evt)
 		return -EINVAL;
 
 	for (i = 0; i < size; ++i) {
-		if (!strcmp(evt, events[i].evt_name))
+		if (!strncmp(evt, events[i].evt_name, strlen(events[i].evt_name)))
 			return events[i].evt_num;
 	}
 
-- 
2.30.1
Re: [PATCH v2 1/2] platform/mellanox: mlxbf-pmc: Replace strcmp with strncmp
Posted by Ilpo Järvinen 3 months, 2 weeks ago
On Wed, 18 Jun 2025, Shravan Kumar Ramani wrote:

> Since the input string passed via the command line appends a newline char,
> comparison using strcmp is not correct. Use the string length of the
> event_list entries to match the string using strncmp instead.

Please include () after any function name (don't forget those in the 
shortlog).

> Fixes: 1a218d312e65 ("platform/mellanox: mlxbf-pmc: Add Mellanox BlueField PMC driver")
> Signed-off-by: Shravan Kumar Ramani <shravankr@nvidia.com>
> Reviewed-by: David Thompson <davthomspson@nvidia.com>
> ---
>  drivers/platform/mellanox/mlxbf-pmc.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/platform/mellanox/mlxbf-pmc.c b/drivers/platform/mellanox/mlxbf-pmc.c
> index 900069eb186e..366c0cba447f 100644
> --- a/drivers/platform/mellanox/mlxbf-pmc.c
> +++ b/drivers/platform/mellanox/mlxbf-pmc.c
> @@ -1215,7 +1215,7 @@ static int mlxbf_pmc_get_event_num(const char *blk, const char *evt)
>  		return -EINVAL;
>  
>  	for (i = 0; i < size; ++i) {
> -		if (!strcmp(evt, events[i].evt_name))
> +		if (!strncmp(evt, events[i].evt_name, strlen(events[i].evt_name)))

So if there's extra garbage behind the input, it will also match 
spuriously? So store the len and reduce it if there's a trailing newline 
to make it more robust?

>  			return events[i].evt_num;
>  	}
>  
> 

-- 
 i.