From patchwork Thu Oct 15 21:06:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 7409961 Return-Path: X-Original-To: patchwork-linux-nvdimm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id EF8229F1D5 for ; Thu, 15 Oct 2015 21:12:31 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2277B2069D for ; Thu, 15 Oct 2015 21:12:31 +0000 (UTC) Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3CA692069F for ; Thu, 15 Oct 2015 21:12:30 +0000 (UTC) Received: from ml01.vlan14.01.org (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 30011605A6; Thu, 15 Oct 2015 14:12:30 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by ml01.01.org (Postfix) with ESMTP id 96B34605A6 for ; Thu, 15 Oct 2015 14:12:29 -0700 (PDT) Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga102.jf.intel.com with ESMTP; 15 Oct 2015 14:12:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.17,687,1437462000"; d="scan'208";a="581677920" Received: from dwillia2-desk3.jf.intel.com ([10.54.39.39]) by FMSMGA003.fm.intel.com with ESMTP; 15 Oct 2015 14:12:15 -0700 Subject: [PATCH 09/11] ndctl: fix NULL check after use in ndctl_dimm_cmd_new_cfg_{read|write}() From: Dan Williams To: linux-nvdimm@lists.01.org Date: Thu, 15 Oct 2015 17:06:31 -0400 Message-ID: <20151015210631.22046.68898.stgit@dwillia2-desk3.jf.intel.com> In-Reply-To: <20151015210544.22046.31483.stgit@dwillia2-desk3.jf.intel.com> References: <20151015210544.22046.31483.stgit@dwillia2-desk3.jf.intel.com> User-Agent: StGit/0.17.1-9-g687f MIME-Version: 1.0 X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Reorder the validation steps of @cfg_size to check if the dimm supports the command after the validity of cfg_size has been established. Signed-off-by: Dan Williams --- lib/libndctl.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/libndctl.c b/lib/libndctl.c index 1149bacf67b9..326b43ec1857 100644 --- a/lib/libndctl.c +++ b/lib/libndctl.c @@ -2021,28 +2021,29 @@ NDCTL_EXPORT struct ndctl_cmd *ndctl_dimm_cmd_new_cfg_read(struct ndctl_cmd *cfg struct ndctl_cmd *cmd; size_t size; - if (!ndctl_dimm_is_cmd_supported(dimm, ND_CMD_GET_CONFIG_DATA)) { - dbg(ctx, "unsupported cmd\n"); - return NULL; - } - if (cfg_size->type != ND_CMD_GET_CONFIG_SIZE || cfg_size->status != 0) { dbg(ctx, "expected sucessfully completed cfg_size command\n"); return NULL; } - if (!cfg_size->dimm || cfg_size->get_size->config_size == 0) { + + if (!dimm || cfg_size->get_size->config_size == 0) { dbg(ctx, "invalid cfg_size\n"); return NULL; } + if (!ndctl_dimm_is_cmd_supported(dimm, ND_CMD_GET_CONFIG_DATA)) { + dbg(ctx, "unsupported cmd\n"); + return NULL; + } + size = sizeof(*cmd) + sizeof(struct nd_cmd_get_config_data_hdr) + cfg_size->get_size->max_xfer; cmd = calloc(1, size); if (!cmd) return NULL; - cmd->dimm = cfg_size->dimm; + cmd->dimm = dimm; cmd->refcount = 1; cmd->type = ND_CMD_GET_CONFIG_DATA; cmd->size = size; @@ -2071,11 +2072,6 @@ NDCTL_EXPORT struct ndctl_cmd *ndctl_dimm_cmd_new_cfg_write(struct ndctl_cmd *cf struct ndctl_cmd *cmd; size_t size; - if (!ndctl_dimm_is_cmd_supported(dimm, ND_CMD_SET_CONFIG_DATA)) { - dbg(ctx, "unsupported cmd\n"); - return NULL; - } - /* enforce rmw */ if (cfg_read->type != ND_CMD_GET_CONFIG_DATA || cfg_read->status != 0) { @@ -2083,18 +2079,23 @@ NDCTL_EXPORT struct ndctl_cmd *ndctl_dimm_cmd_new_cfg_write(struct ndctl_cmd *cf return NULL; } - if (!cfg_read->dimm || cfg_read->get_data->in_length == 0) { + if (!dimm || cfg_read->get_data->in_length == 0) { dbg(ctx, "invalid cfg_read\n"); return NULL; } + if (!ndctl_dimm_is_cmd_supported(dimm, ND_CMD_SET_CONFIG_DATA)) { + dbg(ctx, "unsupported cmd\n"); + return NULL; + } + size = sizeof(*cmd) + sizeof(struct nd_cmd_set_config_hdr) + cfg_read->iter.max_xfer + 4; cmd = calloc(1, size); if (!cmd) return NULL; - cmd->dimm = cfg_read->dimm; + cmd->dimm = dimm; ndctl_cmd_ref(cmd); cmd->type = ND_CMD_SET_CONFIG_DATA; cmd->size = size;