diff mbox series

[ndctl,2/2] libndctl.c: major and minor numbers are unsigned

Message ID 20240820182705.139842-3-jmoyer@redhat.com (mailing list archive)
State Accepted
Commit a461871665a2cb5c81e9db5a850a83af5a6381b1
Headers show
Series [ndctl,1/2] ndctl/keys.c: don't leak fd in error cases | expand

Commit Message

Jeff Moyer Aug. 20, 2024, 6:26 p.m. UTC
From: Jeff Moyer <jmoyer@redhat.com>

Static analysis points out that the cast of bus->major and bus->minor
to a signed type in the call to parent_dev_path could result in a
negative number.  I sincerely doubt we'll see major and minor numbers
that large, but let's fix it.

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
---
 ndctl/lib/libndctl.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Dave Jiang Aug. 20, 2024, 7:59 p.m. UTC | #1
On 8/20/24 11:26 AM, jmoyer@redhat.com wrote:
> From: Jeff Moyer <jmoyer@redhat.com>
> 
> Static analysis points out that the cast of bus->major and bus->minor
> to a signed type in the call to parent_dev_path could result in a
> negative number.  I sincerely doubt we'll see major and minor numbers
> that large, but let's fix it.
> 
> Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>

> ---
>  ndctl/lib/libndctl.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c
> index ddbdd9a..f75dbd4 100644
> --- a/ndctl/lib/libndctl.c
> +++ b/ndctl/lib/libndctl.c
> @@ -710,11 +710,12 @@ NDCTL_EXPORT void ndctl_set_log_priority(struct ndctl_ctx *ctx, int priority)
>  	daxctl_set_log_priority(ctx->daxctl_ctx, priority);
>  }
>  
> -static char *__dev_path(char *type, int major, int minor, int parent)
> +static char *__dev_path(char *type, unsigned int major, unsigned int minor,
> +			int parent)
>  {
>  	char *path, *dev_path;
>  
> -	if (asprintf(&path, "/sys/dev/%s/%d:%d%s", type, major, minor,
> +	if (asprintf(&path, "/sys/dev/%s/%u:%u%s", type, major, minor,
>  				parent ? "/device" : "") < 0)
>  		return NULL;
>  
> @@ -723,7 +724,7 @@ static char *__dev_path(char *type, int major, int minor, int parent)
>  	return dev_path;
>  }
>  
> -static char *parent_dev_path(char *type, int major, int minor)
> +static char *parent_dev_path(char *type, unsigned int major, unsigned int minor)
>  {
>          return __dev_path(type, major, minor, 1);
>  }
Verma, Vishal L Aug. 21, 2024, 9:50 p.m. UTC | #2
On Tue, 2024-08-20 at 14:26 -0400, jmoyer@redhat.com wrote:
> From: Jeff Moyer <jmoyer@redhat.com>
> 
> Static analysis points out that the cast of bus->major and bus->minor
> to a signed type in the call to parent_dev_path could result in a
> negative number.  I sincerely doubt we'll see major and minor numbers
> that large, but let's fix it.
> 
> Signed-off-by: Jeff Moyer <jmoyer@redhat.com>

Looks good,
Reviewed-by: Vishal Verma <vishal.l.verma@intel.com>

> ---
>  ndctl/lib/libndctl.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c
> index ddbdd9a..f75dbd4 100644
> --- a/ndctl/lib/libndctl.c
> +++ b/ndctl/lib/libndctl.c
> @@ -710,11 +710,12 @@ NDCTL_EXPORT void ndctl_set_log_priority(struct
> ndctl_ctx *ctx, int priority)
>  	daxctl_set_log_priority(ctx->daxctl_ctx, priority);
>  }
>  
> -static char *__dev_path(char *type, int major, int minor, int
> parent)
> +static char *__dev_path(char *type, unsigned int major, unsigned int
> minor,
> +			int parent)
>  {
>  	char *path, *dev_path;
>  
> -	if (asprintf(&path, "/sys/dev/%s/%d:%d%s", type, major,
> minor,
> +	if (asprintf(&path, "/sys/dev/%s/%u:%u%s", type, major,
> minor,
>  				parent ? "/device" : "") < 0)
>  		return NULL;
>  
> @@ -723,7 +724,7 @@ static char *__dev_path(char *type, int major,
> int minor, int parent)
>  	return dev_path;
>  }
>  
> -static char *parent_dev_path(char *type, int major, int minor)
> +static char *parent_dev_path(char *type, unsigned int major,
> unsigned int minor)
>  {
>          return __dev_path(type, major, minor, 1);
>  }
diff mbox series

Patch

diff --git a/ndctl/lib/libndctl.c b/ndctl/lib/libndctl.c
index ddbdd9a..f75dbd4 100644
--- a/ndctl/lib/libndctl.c
+++ b/ndctl/lib/libndctl.c
@@ -710,11 +710,12 @@  NDCTL_EXPORT void ndctl_set_log_priority(struct ndctl_ctx *ctx, int priority)
 	daxctl_set_log_priority(ctx->daxctl_ctx, priority);
 }
 
-static char *__dev_path(char *type, int major, int minor, int parent)
+static char *__dev_path(char *type, unsigned int major, unsigned int minor,
+			int parent)
 {
 	char *path, *dev_path;
 
-	if (asprintf(&path, "/sys/dev/%s/%d:%d%s", type, major, minor,
+	if (asprintf(&path, "/sys/dev/%s/%u:%u%s", type, major, minor,
 				parent ? "/device" : "") < 0)
 		return NULL;
 
@@ -723,7 +724,7 @@  static char *__dev_path(char *type, int major, int minor, int parent)
 	return dev_path;
 }
 
-static char *parent_dev_path(char *type, int major, int minor)
+static char *parent_dev_path(char *type, unsigned int major, unsigned int minor)
 {
         return __dev_path(type, major, minor, 1);
 }