From 5f0a857c70863eb685ac03d4183502d3bd460b7c Mon Sep 17 00:00:00 2001 From: "Jose A. Rivera" Date: Wed, 5 Feb 2014 08:50:30 -0600 Subject: libgfapi: Add proper NULL checks Two spots check for NULL after the pointer in question has already been dereferenced. Checked for NULL (and set errno when needed) at appripriate spots, added a few NULL checks further up the stack, and some whitespace cleanup. BUG: 789278 CID: 1124800 CID: 1124805 Change-Id: I1dd4ad3f285cca36f4e3f739288f154ec120aebb Signed-off-by: Jose A. Rivera Reviewed-on: http://review.gluster.org/6908 Reviewed-by: Shyamsundar Ranganathan Tested-by: Gluster Build System Reviewed-by: Vijay Bellur --- api/src/glfs-mgmt.c | 48 ++++++++++++++++++++++++++++++------------------ api/src/glfs.c | 14 ++++++++++++++ 2 files changed, 44 insertions(+), 18 deletions(-) (limited to 'api/src') diff --git a/api/src/glfs-mgmt.c b/api/src/glfs-mgmt.c index 6843e9cb3fe..afb351da03a 100644 --- a/api/src/glfs-mgmt.c +++ b/api/src/glfs-mgmt.c @@ -236,11 +236,19 @@ mgmt_getspec_cbk (struct rpc_req *req, struct iovec *iov, int count, int ret = 0; ssize_t size = 0; FILE *tmpfp = NULL; - int need_retry = 0; + int need_retry = 0; struct glfs *fs = NULL; frame = myframe; ctx = frame->this->ctx; + + if (!ctx) { + gf_log (frame->this->name, GF_LOG_ERROR, "NULL context"); + errno = EINVAL; + ret = -1; + goto out; + } + fs = ((xlator_t *)ctx->master)->private; if (-1 == req->rpc_status) { @@ -260,7 +268,7 @@ mgmt_getspec_cbk (struct rpc_req *req, struct iovec *iov, int count, gf_log (frame->this->name, GF_LOG_ERROR, "failed to get the 'volume file' from server"); ret = -1; - errno = rsp.op_errno; + errno = rsp.op_errno; goto out; } @@ -296,7 +304,7 @@ mgmt_getspec_cbk (struct rpc_req *req, struct iovec *iov, int count, */ ret = glusterfs_volfile_reconfigure (fs->oldvollen, tmpfp, fs->ctx, - fs->oldvolfile); + fs->oldvolfile); if (ret == 0) { gf_log ("glusterfsd-mgmt", GF_LOG_DEBUG, "No need to re-load volfile, reconfigure done"); @@ -323,13 +331,13 @@ out: if (rsp.spec) free (rsp.spec); - // Stop if server is running at an unsupported op-version - if (ENOTSUP == ret) { - gf_log ("mgmt", GF_LOG_ERROR, "Server is operating at an " - "op-version which is not supported"); - errno = ENOTSUP; - glfs_init_done (fs, -1); - } + // Stop if server is running at an unsupported op-version + if (ENOTSUP == ret) { + gf_log ("mgmt", GF_LOG_ERROR, "Server is operating at an " + "op-version which is not supported"); + errno = ENOTSUP; + glfs_init_done (fs, -1); + } if (ret && ctx && !ctx->active) { /* Do it only for the first time */ @@ -339,10 +347,10 @@ out: "failed to fetch volume file (key:%s)", ctx->cmd_args.volfile_id); if (!need_retry) { - if (!errno) - errno = EINVAL; + if (!errno) + errno = EINVAL; glfs_init_done (fs, -1); - } + } } if (tmpfp) @@ -420,6 +428,10 @@ mgmt_rpc_notify (struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event, this = mydata; ctx = this->ctx; + + if (!ctx) + goto out; + fs = ((xlator_t *)ctx->master)->private; cmd_args = &ctx->cmd_args; @@ -434,22 +446,22 @@ mgmt_rpc_notify (struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event, "%d connect attempts left", cmd_args->max_connect_attempts); if (0 >= cmd_args->max_connect_attempts) { - errno = ENOTCONN; + errno = ENOTCONN; glfs_init_done (fs, -1); - } + } } break; case RPC_CLNT_CONNECT: rpc_clnt_set_connected (&((struct rpc_clnt*)ctx->mgmt)->conn); ret = glfs_volfile_fetch (fs); - if (ret && ctx && (ctx->active == NULL)) { + if (ret && (ctx->active == NULL)) { /* Do it only for the first time */ /* Exit the process.. there are some wrong options */ gf_log ("glfs-mgmt", GF_LOG_ERROR, "failed to fetch volume file (key:%s)", ctx->cmd_args.volfile_id); - errno = EINVAL; + errno = EINVAL; glfs_init_done (fs, -1); } @@ -457,7 +469,7 @@ mgmt_rpc_notify (struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event, default: break; } - +out: return 0; } diff --git a/api/src/glfs.c b/api/src/glfs.c index 174076e390f..ea10fb55c65 100644 --- a/api/src/glfs.c +++ b/api/src/glfs.c @@ -589,6 +589,13 @@ glfs_init_async (struct glfs *fs, glfs_init_cbk cbk) { int ret = -1; + if (!fs || !fs->ctx) { + gf_log ("glfs", GF_LOG_ERROR, + "fs is not properly initialized."); + errno = EINVAL; + return ret; + } + fs->init_cbk = cbk; ret = glfs_init_common (fs); @@ -602,6 +609,13 @@ glfs_init (struct glfs *fs) { int ret = -1; + if (!fs || !fs->ctx) { + gf_log ("glfs", GF_LOG_ERROR, + "fs is not properly initialized."); + errno = EINVAL; + return ret; + } + ret = glfs_init_common (fs); if (ret) return ret; -- cgit