nvdimm.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Zhiqiang Liu <liuzhiqiang26@huawei.com>
To: <vishal.l.verma@intel.com>
Cc: linux-nvdimm@lists.01.org, linfeilong <linfeilong@huawei.com>
Subject: [ndctl PATCH 7/8] Check whether ndctl_btt_get_namespace returns NULL in callers
Date: Fri, 6 Nov 2020 17:27:35 +0800	[thread overview]
Message-ID: <ef7e2548-0fdf-8472-83bc-7bcac67b4398@huawei.com> (raw)
In-Reply-To: <bed3b3b3-1f30-6751-c0bf-15ecf70192f9@huawei.com>


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 --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;
-- 
1.8.3.1


_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-leave@lists.01.org

  parent reply	other threads:[~2020-11-06  9:27 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-06  9:23 [ndctl PATCH 0/8] fix serverl issues reported by Coverity Zhiqiang Liu
2020-11-06  9:24 ` [ndctl PATCH 1/8] namespace: check whether pfn|dax|btt is NULL in setup_namespace Zhiqiang Liu
2020-11-20 16:29   ` Jeff Moyer
2020-11-06  9:25 ` [ndctl PATCH 2/8] lib/libndctl: fix memory leakage problem in add_bus Zhiqiang Liu
2020-11-20 16:31   ` Jeff Moyer
2020-11-06  9:25 ` [ndctl PATCH 3/8] libdaxctl: fix memory leakage in add_dax_region() Zhiqiang Liu
2020-11-20 16:33   ` Jeff Moyer
2020-11-06  9:26 ` [ndctl PATCH 4/8] dimm: fix potential fd leakage in dimm_action() Zhiqiang Liu
2020-11-20 16:36   ` Jeff Moyer
2020-11-06  9:26 ` [ndctl PATCH 5/8] util/help: check whether strdup returns NULL in exec_man_konqueror Zhiqiang Liu
2020-11-06  9:27 ` [ndctl PATCH 6/8] lib/inject: check whether cmd is created successfully Zhiqiang Liu
2020-12-17  3:33   ` Verma, Vishal L
2020-12-17  6:19     ` Zhiqiang Liu
2020-11-06  9:27 ` Zhiqiang Liu [this message]
2020-11-06  9:28 ` [ndctl PATCH 8/8] Check whether seed is NULL in validate_namespace_options Zhiqiang Liu
2020-11-10 10:58 ` [ndctl PATCH 0/8] fix serverl issues reported by Coverity Zhiqiang Liu
2020-11-20  2:45 ` Zhiqiang Liu
2020-11-20  2:47   ` Verma, Vishal L
2020-11-20  3:01     ` Zhiqiang Liu
2020-11-20 16:45 ` Jeff Moyer
2020-11-23  0:47   ` Zhiqiang Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ef7e2548-0fdf-8472-83bc-7bcac67b4398@huawei.com \
    --to=liuzhiqiang26@huawei.com \
    --cc=linfeilong@huawei.com \
    --cc=linux-nvdimm@lists.01.org \
    --cc=vishal.l.verma@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).