Skip to contentSkip to conversation

cxl: static analysis fixes

PATCH In review Vishal Verma 4 participants 12 messages Aug 23, 2022 – Aug 24, 2022 libnvdimm: non-volatile memory device

lore
Share & tools
lore

Vishal Verma

author maintainer
#0 lore raw

v2 cover cxl: static analysis fixes

Changes since v1[1]:
- Fix the decoder filter check in patch 1.
- Fix a missed free(path) in patch 2.

[1]: https://lore.kernel.org/linux-cxl/[email protected]

---

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 | 4 +++-
 cxl/filter.c     | 2 +-
 cxl/region.c     | 5 ++---
 3 files changed, 6 insertions(+), 5 deletions(-)

base-commit: 9a993ce24fdd5de45774b65211570dd514cdf61d

Signature · 1 line
2.37.2
author maintainer
#1 lore raw

PATCH v2 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(). The NULL check is valid because it was the filter result being checked, however, while doing this, the original 'decoder' variable was being clobbered.

Check the filter results independently of the original decoder variable.

---
 cxl/region.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
cxl/region.c +2 -3
index a30313c..334fcc2 100644
@@ -686,9 +686,8 @@ static int region_action(int argc, const char **argv, struct cxl_ctx *ctx,
 			continue;
 
 		cxl_decoder_foreach (port, decoder) {
-			decoder = util_cxl_decoder_filter(decoder,
-							  param.root_decoder);
-			if (!decoder)
+			if (!util_cxl_decoder_filter(decoder,
+						     param.root_decoder))
 				continue;
 			rc = decoder_region_action(p, decoder, action, count);
 			if (rc)
Signature · 1 line
2.37.2
Reviewed-by on v2 1/3
#2 lore raw

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

Quoted 18 lines · Vishal Verma

A NULL check in region_action() implies that 'decoder' might be NULL, but later we dereference it during cxl_decoder_foreach(). The NULL check is valid because it was the filter result being checked, however, while doing this, the original 'decoder' variable was being clobbered.

Check the filter results independently of the original decoder variable.

---
 cxl/region.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
cxl/region.c +0 -0
index a30313c..334fcc2 100644

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

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

Looks good.

maintainer Reviewed-by on v2 1/3
#3 lore raw

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

In reply to Vishal Verma
Quoted 9 lines · Vishal Verma

A NULL check in region_action() implies that 'decoder' might be NULL, but later we dereference it during cxl_decoder_foreach(). The NULL check is valid because it was the filter result being checked, however, while doing this, the original 'decoder' variable was being clobbered.

Check the filter results independently of the original decoder variable.

Quoted 20 lines
---
  cxl/region.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
cxl/region.c +2 -3
index a30313c..334fcc2 100644
@@ -686,9 +686,8 @@ static int region_action(int argc, const char **argv, struct cxl_ctx *ctx,
  			continue;
 
  		cxl_decoder_foreach (port, decoder) {
-			decoder = util_cxl_decoder_filter(decoder,
-							  param.root_decoder);
-			if (!decoder)
+			if (!util_cxl_decoder_filter(decoder,
+						     param.root_decoder))
  				continue;
  			rc = decoder_region_action(p, decoder, action, count);
  			if (rc)
#4 lore raw

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

In reply to Vishal Verma
Quoted 9 lines · On Tue, 23 Aug 2022 01:45:25 -0600 Vishal Verma <[email protected]>…

A NULL check in region_action() implies that 'decoder' might be NULL, but later we dereference it during cxl_decoder_foreach(). The NULL check is valid because it was the filter result being checked, however, while doing this, the original 'decoder' variable was being clobbered.

Check the filter results independently of the original decoder variable.

Typo in patch title.

Quoted 20 lines
---
 cxl/region.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
cxl/region.c +2 -3
index a30313c..334fcc2 100644
@@ -686,9 +686,8 @@ static int region_action(int argc, const char **argv, struct cxl_ctx *ctx,
 			continue;
 
 		cxl_decoder_foreach (port, decoder) {
-			decoder = util_cxl_decoder_filter(decoder,
-							  param.root_decoder);
-			if (!decoder)
+			if (!util_cxl_decoder_filter(decoder,
+						     param.root_decoder))
 				continue;
 			rc = decoder_region_action(p, decoder, action, count);
 			if (rc)
author maintainer
#5 lore raw

PATCH v2 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.

Add anew error label to just free 'path' and return for this exit case.

---
 cxl/lib/libcxl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
cxl/lib/libcxl.c +3 -1
index 021d59f..e8c5d44 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;
+		goto err_path;
 
 	region->id = id;
 	region->ctx = ctx;
@@ -551,11 +551,13 @@ 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);
 	free(region->dev_buf);
 	free(region);
+err_path:
 	free(path);
 	return NULL;
 }
Signature · 1 line
2.37.2
Reviewed-by on v2 2/3
#6 lore raw

RE: [ndctl PATCH v2 2/3] libcxl: fox a resource leak and a forward NULL check

Quoted 6 lines · 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.

Add anew error label to just free 'path' and return for this exit case.

s/anew/a new/

maintainer Reviewed-by on v2 2/3
#7 lore raw

Re: [ndctl PATCH v2 2/3] libcxl: fox a resource leak and a forward NULL check

In reply to Vishal Verma
Quoted 9 lines · 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.

Add anew error label to just free 'path' and return for this exit case.

Quoted 31 lines
---
  cxl/lib/libcxl.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
cxl/lib/libcxl.c +3 -1
index 021d59f..e8c5d44 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;
+		goto err_path;
 
  	region->id = id;
  	region->ctx = ctx;
@@ -551,11 +551,13 @@ 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);
  	free(region->dev_buf);
  	free(region);
+err_path:
  	free(path);
  	return NULL;
  }
author maintainer
#8 lore raw

PATCH v2 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
Reviewed-by on v2 3/3
#9 lore raw

RE: [ndctl PATCH v2 3/3] cxl/filter: Fix an uninitialized pointer dereference

Quoted 15 lines · 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 +0 -0
index 9a3de8c..56c6599 100644

cxl/filter.c static void walk_decoders(struct cxl_port *port, struct cxl_filter_params *p, · line 799

Quoted 5 lines
cxl/filter.c +1 -1
@@ -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;

Looks good,

maintainer Reviewed-by on v2 3/3
#10 lore raw

Re: [ndctl PATCH v2 3/3] cxl/filter: Fix an uninitialized pointer dereference

In reply to Vishal Verma
Quoted 6 lines · 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.

Quoted 17 lines
---
  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;
Reviewed-by on the v2 series
#11 lore raw
In reply to Vishal Verma
Quoted 5 lines · On Tue, 23 Aug 2022 01:45:24 -0600 Vishal Verma <[email protected]>…

Changes since v1[1]:
- Fix the decoder filter check in patch 1.
- Fix a missed free(path) in patch 2.

[1]: https://lore.kernel.org/linux-cxl/[email protected]

FWIW: Series looks fine to me.

Quoted 18 lines

---

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 | 4 +++-
 cxl/filter.c     | 2 +-
 cxl/region.c     | 5 ++---
 3 files changed, 6 insertions(+), 5 deletions(-)

base-commit: 9a993ce24fdd5de45774b65211570dd514cdf61d