From patchwork Fri Nov 6 09:27:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhiqiang Liu X-Patchwork-Id: 11886489 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E630F697 for ; Fri, 6 Nov 2020 09:27:50 +0000 (UTC) Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 773FD208B3 for ; Fri, 6 Nov 2020 09:27:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 773FD208B3 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-nvdimm-bounces@lists.01.org Received: from ml01.vlan13.01.org (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id D829E1674C102; Fri, 6 Nov 2020 01:27:49 -0800 (PST) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=45.249.212.190; helo=szxga04-in.huawei.com; envelope-from=liuzhiqiang26@huawei.com; receiver= Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id D4E241674C0FD for ; Fri, 6 Nov 2020 01:27:45 -0800 (PST) Received: from DGGEMS410-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4CSFRF39GMzkbyN for ; Fri, 6 Nov 2020 17:27:37 +0800 (CST) Received: from [127.0.0.1] (10.174.176.238) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server id 14.3.487.0; Fri, 6 Nov 2020 17:27:36 +0800 Subject: [ndctl PATCH 7/8] Check whether ndctl_btt_get_namespace returns NULL in callers To: References: From: Zhiqiang Liu Message-ID: Date: Fri, 6 Nov 2020 17:27:35 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US X-Originating-IP: [10.174.176.238] X-CFilter-Loop: Reflected Message-ID-Hash: VA55HJAXANYXP35DC5RSGTXFHC363OFC X-Message-ID-Hash: VA55HJAXANYXP35DC5RSGTXFHC363OFC X-MailFrom: liuzhiqiang26@huawei.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: linux-nvdimm@lists.01.org, linfeilong X-Mailman-Version: 3.1.1 Precedence: list List-Id: "Linux-nvdimm developer list." Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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;