diff mbox series

[ndctl,7/8] Check whether ndctl_btt_get_namespace returns NULL in callers

Message ID ef7e2548-0fdf-8472-83bc-7bcac67b4398@huawei.com (mailing list archive)
State New, archived
Headers show
Series fix serverl issues reported by Coverity | expand

Commit Message

Zhiqiang Liu Nov. 6, 2020, 9:27 a.m. UTC
ndctl_btt_get_namespace() may return NULL, so we need to check
return value of ndctl_btt_get_namespace() before using the return
value in callers.

Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
---
 test/libndctl.c    | 16 +++++++++++-----
 test/parent-uuid.c |  2 +-
 util/json.c        |  3 +++
 3 files changed, 15 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/test/libndctl.c b/test/libndctl.c
index 994e0fa..a8db388 100644
--- a/test/libndctl.c
+++ b/test/libndctl.c
@@ -983,13 +983,19 @@  static int check_pfn_create(struct ndctl_region *region,

 static int check_btt_size(struct ndctl_btt *btt)
 {
+	unsigned long long ns_size;
+	unsigned long sect_size;
+	unsigned long long actual, expect;
+	int size_select, sect_select;
 	struct ndctl_ctx *ctx = ndctl_btt_get_ctx(btt);
 	struct ndctl_test *test = ndctl_get_private_data(ctx);
 	struct ndctl_namespace *ndns = ndctl_btt_get_namespace(btt);
-	unsigned long long ns_size = ndctl_namespace_get_size(ndns);
-	unsigned long sect_size = ndctl_btt_get_sector_size(btt);
-	unsigned long long actual, expect;
-	int size_select, sect_select;
+
+	if (!ndns)
+		return -ENXIO;
+
+	ns_size = ndctl_namespace_get_size(ndns);
+	sect_size = ndctl_btt_get_sector_size(btt);
 	unsigned long long expect_table[][2] = {
 		[0] = {
 			[0] = 0x11b5400,
@@ -1461,7 +1467,7 @@  static int check_btt_autodetect(struct ndctl_bus *bus,
 		if (!ndctl_btt_is_enabled(btt))
 			continue;
 		btt_ndns = ndctl_btt_get_namespace(btt);
-		if (strcmp(ndctl_namespace_get_devname(btt_ndns), devname) != 0)
+		if (!btt_ndns || strcmp(ndctl_namespace_get_devname(btt_ndns), devname) != 0)
 			continue;
 		fprintf(stderr, "%s: btt_ndns: %p ndns: %p\n", __func__,
 				btt_ndns, ndns);
diff --git a/test/parent-uuid.c b/test/parent-uuid.c
index f41ca2c..303b30b 100644
--- a/test/parent-uuid.c
+++ b/test/parent-uuid.c
@@ -115,7 +115,7 @@  static struct ndctl_btt *check_valid_btt(struct ndctl_region *region,
 		if (!ndctl_btt_is_enabled(btt))
 			continue;
 		btt_ndns = ndctl_btt_get_namespace(btt);
-		if (strcmp(ndctl_namespace_get_devname(btt_ndns),
+		if (!btt_ndns || strcmp(ndctl_namespace_get_devname(btt_ndns),
 				ndctl_namespace_get_devname(ndns)) != 0)
 			continue;
 		return btt;
diff --git a/util/json.c b/util/json.c
index 77bd478..1392403 100644
--- a/util/json.c
+++ b/util/json.c
@@ -1004,6 +1004,9 @@  static void util_btt_badblocks_to_json(struct ndctl_btt *btt,
 	struct ndctl_namespace *ndns = ndctl_btt_get_namespace(btt);
 	unsigned long long begin, size;

+	if (!ndns)
+		return;
+
 	begin = ndctl_namespace_get_resource(ndns);
 	if (begin == ULLONG_MAX)
 		return;