diff mbox series

[ndctl,v2,3/5] ndctl/dimm: do not increment a ULLONG_MAX slot value

Message ID 04880bb53cbd400d9906ca2ac5042a9dc23b925f.1741304303.git.alison.schofield@intel.com (mailing list archive)
State New
Headers show
Series Address Coverity Scan Defects | expand

Commit Message

Alison Schofield March 6, 2025, 11:50 p.m. UTC
From: Alison Schofield <alison.schofield@intel.com>

A coverity scan higlighted an overflow issue when the slot variable,
an unsigned integer that is initialized to -1, is incremented and
overflows.

Initialize slot to 0 and increment slot in the for loop header. That
keeps the comparison to a u32 as is and avoids overflow.

Signed-off-by: Alison Schofield <alison.schofield@intel.com>
---
 ndctl/dimm.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

Dave Jiang March 7, 2025, 9:43 p.m. UTC | #1
On 3/6/25 4:50 PM, alison.schofield@intel.com wrote:
> From: Alison Schofield <alison.schofield@intel.com>
> 
> A coverity scan higlighted an overflow issue when the slot variable,
> an unsigned integer that is initialized to -1, is incremented and
> overflows.
> 
> Initialize slot to 0 and increment slot in the for loop header. That
> keeps the comparison to a u32 as is and avoids overflow.
> 
> Signed-off-by: Alison Schofield <alison.schofield@intel.com>

Reviewed-by: Dave Jiang <dave.jiang@intel.com>

> ---
>  ndctl/dimm.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/ndctl/dimm.c b/ndctl/dimm.c
> index 889b620355fc..aaa0abfa046c 100644
> --- a/ndctl/dimm.c
> +++ b/ndctl/dimm.c
> @@ -97,7 +97,7 @@ static struct json_object *dump_label_json(struct ndctl_dimm *dimm,
>  	struct json_object *jlabel = NULL;
>  	struct namespace_label nslabel;
>  	unsigned int nsindex_size;
> -	unsigned int slot = -1;
> +	unsigned int slot = 0;
>  	ssize_t offset;
>  
>  	if (!jarray)
> @@ -108,14 +108,13 @@ static struct json_object *dump_label_json(struct ndctl_dimm *dimm,
>  		return NULL;
>  
>  	for (offset = nsindex_size * 2; offset < size;
> -			offset += ndctl_dimm_sizeof_namespace_label(dimm)) {
> +	     offset += ndctl_dimm_sizeof_namespace_label(dimm), slot++) {
>  		ssize_t len = min_t(ssize_t,
>  				ndctl_dimm_sizeof_namespace_label(dimm),
>  				size - offset);
>  		struct json_object *jobj;
>  		char uuid[40];
>  
> -		slot++;
>  		jlabel = json_object_new_object();
>  		if (!jlabel)
>  			break;
diff mbox series

Patch

diff --git a/ndctl/dimm.c b/ndctl/dimm.c
index 889b620355fc..aaa0abfa046c 100644
--- a/ndctl/dimm.c
+++ b/ndctl/dimm.c
@@ -97,7 +97,7 @@  static struct json_object *dump_label_json(struct ndctl_dimm *dimm,
 	struct json_object *jlabel = NULL;
 	struct namespace_label nslabel;
 	unsigned int nsindex_size;
-	unsigned int slot = -1;
+	unsigned int slot = 0;
 	ssize_t offset;
 
 	if (!jarray)
@@ -108,14 +108,13 @@  static struct json_object *dump_label_json(struct ndctl_dimm *dimm,
 		return NULL;
 
 	for (offset = nsindex_size * 2; offset < size;
-			offset += ndctl_dimm_sizeof_namespace_label(dimm)) {
+	     offset += ndctl_dimm_sizeof_namespace_label(dimm), slot++) {
 		ssize_t len = min_t(ssize_t,
 				ndctl_dimm_sizeof_namespace_label(dimm),
 				size - offset);
 		struct json_object *jobj;
 		char uuid[40];
 
-		slot++;
 		jlabel = json_object_new_object();
 		if (!jlabel)
 			break;