Skip to contentSkip to conversation

cxl: static analysis fixes

PATCH Superseded Vishal Verma 1 participant 5 messages Aug 23, 2022 libnvdimm: non-volatile memory device

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Vishal Verma

author maintainer
#0 lore raw

cover cxl: static analysis fixes

Fix a small handful of issues reported by scan.coverity.com for the recent region management additions.

Vishal Verma (3):
cxl/region: fix a dereferecnce after NULL check
libcxl: fox a resource leak and a forward NULL check cxl/filter: Fix an uninitialized pointer dereference

 cxl/lib/libcxl.c | 3 ++-
 cxl/filter.c     | 2 +-
 cxl/region.c     | 2 --
 3 files changed, 3 insertions(+), 4 deletions(-)

base-commit: 9a993ce24fdd5de45774b65211570dd514cdf61d

Signature · 1 line
2.37.2
author maintainer
#1 lore raw

PATCH 1/3 cxl/region: fix a dereferecnce after NULL check

A NULL check in region_action() implies that 'decoder' might be NULL, but later we dereference it during cxl_decoder_foreach().

Since cxl_decoder_foreach() won't ever enter the loop with a NULL decoder, the check was superfluous. Remove it.

---
 cxl/region.c | 2 --
 1 file changed, 2 deletions(-)
cxl/region.c +0 -2
index a30313c..9372d6b 100644
@@ -688,8 +688,6 @@ static int region_action(int argc, const char **argv, struct cxl_ctx *ctx,
 		cxl_decoder_foreach (port, decoder) {
 			decoder = util_cxl_decoder_filter(decoder,
 							  param.root_decoder);
-			if (!decoder)
-				continue;
 			rc = decoder_region_action(p, decoder, action, count);
 			if (rc)
 				err_rc = rc;
Signature · 1 line
2.37.2
author maintainer
#2 lore raw

Re: [ndctl PATCH 1/3] cxl/region: fix a dereferecnce after NULL check

Quoted 17 lines · Vishal Verma

A NULL check in region_action() implies that 'decoder' might be NULL, but later we dereference it during cxl_decoder_foreach().

Since cxl_decoder_foreach() won't ever enter the loop with a NULL decoder, the check was superfluous. Remove it.

---
 cxl/region.c | 2 --

 1 file changed, 2 deletions(-)
cxl/region.c +0 -0
index a30313c..9372d6b 100644

cxl/region.c static int region_action(int argc, const char **argv, struct cxl_ctx *ctx, · line 692

Quoted 5 lines
cxl/region.c +0 -2
@@ -688,8 +688,6 @@ static int region_action(int argc, const char **argv, struct cxl_ctx *ctx,
                 cxl_decoder_foreach (port, decoder) {
                         decoder = util_cxl_decoder_filter(decoder,
                                                           param.root_decoder);
-                       if (!decoder)
-                               continue;

Hm, this is actually wrong. We need to save the filter results in a new variable, and NULL check that, while keeping the original 'decoder' variable intact for the loop. I'll send v2.

Quoted 3 lines
cxl/region.c +0 -0
                         rc = decoder_region_action(p, decoder, action, count);
                         if (rc)
                                 err_rc = rc;
author maintainer
#3 lore raw

PATCH 2/3 libcxl: fox a resource leak and a forward NULL check

In reply to Vishal Verma

Static analysis reports a couple of issues in add_cxl_region(). Firstly, 'path' wasn't freed in the success case, only in the error case. Secondly, the error handling after 'calloc()'ing the region object erroneously jumped to the error path which tried to free the region object, instead of directly returning NULL.

---
 cxl/lib/libcxl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
cxl/lib/libcxl.c +2 -1
index 021d59f..9945fd1 100644
@@ -482,7 +482,7 @@ static void *add_cxl_region(void *parent, int id, const char *cxlregion_base)
 
 	region = calloc(1, sizeof(*region));
 	if (!region)
-		goto err;
+		return NULL;
 
 	region->id = id;
 	region->ctx = ctx;
@@ -551,6 +551,7 @@ static void *add_cxl_region(void *parent, int id, const char *cxlregion_base)
 
 	list_add_sorted(&decoder->regions, region, list, region_start_cmp);
 
+	free(path);
 	return region;
 err:
 	free(region->dev_path);
Signature · 1 line
2.37.2
author maintainer
#4 lore raw

PATCH 3/3 cxl/filter: Fix an uninitialized pointer dereference

In reply to Vishal Verma

Static analysis points out that there was a chance that 'jdecoder' could be used while uninitialized in walk_decoders(). Initialize it to NULL to avoid this.

---
 cxl/filter.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cxl/filter.c +1 -1
index 9a3de8c..56c6599 100644
@@ -796,7 +796,7 @@ static void walk_decoders(struct cxl_port *port, struct cxl_filter_params *p,
 	cxl_decoder_foreach(port, decoder) {
 		const char *devname = cxl_decoder_get_devname(decoder);
 		struct json_object *jchildregions = NULL;
-		struct json_object *jdecoder;
+		struct json_object *jdecoder = NULL;
 
 		if (!p->decoders)
 			goto walk_children;
Signature · 1 line
2.37.2