diff mbox

[09/11] ndctl: fix NULL check after use in ndctl_dimm_cmd_new_cfg_{read|write}()

Message ID 20151015210631.22046.68898.stgit@dwillia2-desk3.jf.intel.com (mailing list archive)
State Accepted
Commit f2849a154ed4
Headers show

Commit Message

Dan Williams Oct. 15, 2015, 9:06 p.m. UTC
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 <dan.j.williams@intel.com>
---
 lib/libndctl.c |   29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
diff mbox

Patch

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;