256ebe
From 0021a4bbc9af2bfe28d4a79f76c3cd33f23dd118 Mon Sep 17 00:00:00 2001
256ebe
From: Mohammed Rafi KC <rkavunga@redhat.com>
256ebe
Date: Fri, 5 Apr 2019 12:33:55 +0530
256ebe
Subject: [PATCH 101/124] shd/mux: Fix coverity issues introduced by shd mux
256ebe
 patch
256ebe
256ebe
CID 1400475:  Null pointer dereferences  (FORWARD_NULL)
256ebe
CID 1400474:  Null pointer dereferences  (FORWARD_NULL)
256ebe
CID 1400471:  Code maintainability issues  (UNUSED_VALUE)
256ebe
CID 1400470:  Null pointer dereferences  (FORWARD_NULL)
256ebe
CID 1400469:  Memory - illegal accesses  (USE_AFTER_FREE)
256ebe
CID 1400467:  Code maintainability issues  (UNUSED_VALUE)
256ebe
256ebe
Backport of: https://review.gluster.org/#/c/glusterfs/+/22514/
256ebe
256ebe
>Change-Id: I0ca1c733be335c6e5844f44850f8066626ac40d4
256ebe
>updates: bz#789278
256ebe
>Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
256ebe
256ebe
Change-Id: I0425efca9ab5a95801eff9e99259219449a16380
256ebe
BUG: 1471742
256ebe
Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
256ebe
Reviewed-on: https://code.engineering.redhat.com/gerrit/167832
256ebe
Tested-by: RHGS Build Bot <nigelb@redhat.com>
256ebe
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
256ebe
---
256ebe
 libglusterfs/src/graph.c                        | 21 +++++++++++++--------
256ebe
 xlators/mgmt/glusterd/src/glusterd-shd-svc.c    |  6 ++++++
256ebe
 xlators/mgmt/glusterd/src/glusterd-svc-helper.c | 24 +++++++++++++++++-------
256ebe
 3 files changed, 36 insertions(+), 15 deletions(-)
256ebe
256ebe
diff --git a/libglusterfs/src/graph.c b/libglusterfs/src/graph.c
256ebe
index a492dd8..4c8b02d 100644
256ebe
--- a/libglusterfs/src/graph.c
256ebe
+++ b/libglusterfs/src/graph.c
256ebe
@@ -1470,7 +1470,9 @@ glusterfs_process_svc_detach(glusterfs_ctx_t *ctx, gf_volfile_t *volfile_obj)
256ebe
         goto out;
256ebe
     parent_graph = ctx->active;
256ebe
     graph = volfile_obj->graph;
256ebe
-    if (graph && graph->first)
256ebe
+    if (!graph)
256ebe
+        goto out;
256ebe
+    if (graph->first)
256ebe
         xl = graph->first;
256ebe
 
256ebe
     last_xl = graph->last_xl;
256ebe
@@ -1591,12 +1593,10 @@ glusterfs_process_svc_attach_volfp(glusterfs_ctx_t *ctx, FILE *fp,
256ebe
     parent_graph->leaf_count += graph->leaf_count;
256ebe
     parent_graph->id++;
256ebe
 
256ebe
+    volfile_obj = GF_CALLOC(1, sizeof(gf_volfile_t), gf_common_volfile_t);
256ebe
     if (!volfile_obj) {
256ebe
-        volfile_obj = GF_CALLOC(1, sizeof(gf_volfile_t), gf_common_volfile_t);
256ebe
-        if (!volfile_obj) {
256ebe
-            ret = -1;
256ebe
-            goto out;
256ebe
-        }
256ebe
+        ret = -1;
256ebe
+        goto out;
256ebe
     }
256ebe
 
256ebe
     graph->used = 1;
256ebe
@@ -1641,6 +1641,7 @@ glusterfs_mux_volfile_reconfigure(FILE *newvolfile_fp, glusterfs_ctx_t *ctx,
256ebe
 {
256ebe
     glusterfs_graph_t *oldvolfile_graph = NULL;
256ebe
     glusterfs_graph_t *newvolfile_graph = NULL;
256ebe
+    char vol_id[NAME_MAX + 1];
256ebe
 
256ebe
     int ret = -1;
256ebe
 
256ebe
@@ -1672,6 +1673,9 @@ glusterfs_mux_volfile_reconfigure(FILE *newvolfile_fp, glusterfs_ctx_t *ctx,
256ebe
     glusterfs_graph_prepare(newvolfile_graph, ctx, newvolfile_graph->first);
256ebe
 
256ebe
     if (!is_graph_topology_equal(oldvolfile_graph, newvolfile_graph)) {
256ebe
+        ret = snprintf(vol_id, sizeof(vol_id), "%s", volfile_obj->vol_id);
256ebe
+        if (ret < 0)
256ebe
+            goto out;
256ebe
         ret = glusterfs_process_svc_detach(ctx, volfile_obj);
256ebe
         if (ret) {
256ebe
             gf_msg("glusterfsd-mgmt", GF_LOG_ERROR, EINVAL,
256ebe
@@ -1680,8 +1684,9 @@ glusterfs_mux_volfile_reconfigure(FILE *newvolfile_fp, glusterfs_ctx_t *ctx,
256ebe
                    "old graph. Aborting the reconfiguration operation");
256ebe
             goto out;
256ebe
         }
256ebe
-        ret = glusterfs_process_svc_attach_volfp(ctx, newvolfile_fp,
256ebe
-                                                 volfile_obj->vol_id, checksum);
256ebe
+        volfile_obj = NULL;
256ebe
+        ret = glusterfs_process_svc_attach_volfp(ctx, newvolfile_fp, vol_id,
256ebe
+                                                 checksum);
256ebe
         goto out;
256ebe
     }
256ebe
 
256ebe
diff --git a/xlators/mgmt/glusterd/src/glusterd-shd-svc.c b/xlators/mgmt/glusterd/src/glusterd-shd-svc.c
256ebe
index 937ea30..04a4b2e 100644
256ebe
--- a/xlators/mgmt/glusterd/src/glusterd-shd-svc.c
256ebe
+++ b/xlators/mgmt/glusterd/src/glusterd-shd-svc.c
256ebe
@@ -101,6 +101,8 @@ glusterd_shdsvc_init(void *data, glusterd_conn_t *mux_conn,
256ebe
         svc->conn.rpc = rpc_clnt_ref(mux_svc->rpc);
256ebe
         ret = snprintf(svc->conn.sockpath, sizeof(svc->conn.sockpath), "%s",
256ebe
                        mux_conn->sockpath);
256ebe
+        if (ret < 0)
256ebe
+            goto out;
256ebe
     } else {
256ebe
         ret = mkdir_p(logdir, 0755, _gf_true);
256ebe
         if ((ret == -1) && (EEXIST != errno)) {
256ebe
@@ -663,6 +665,10 @@ glusterd_shdsvc_stop(glusterd_svc_t *svc, int sig)
256ebe
         glusterd_volinfo_ref(volinfo);
256ebe
         svc_proc->data = volinfo;
256ebe
         ret = glusterd_svc_stop(svc, sig);
256ebe
+        if (ret) {
256ebe
+            glusterd_volinfo_unref(volinfo);
256ebe
+            goto out;
256ebe
+        }
256ebe
     }
256ebe
     if (!empty && pid != -1) {
256ebe
         ret = glusterd_detach_svc(svc, volinfo, sig);
256ebe
diff --git a/xlators/mgmt/glusterd/src/glusterd-svc-helper.c b/xlators/mgmt/glusterd/src/glusterd-svc-helper.c
256ebe
index e42703c..02945b1 100644
256ebe
--- a/xlators/mgmt/glusterd/src/glusterd-svc-helper.c
256ebe
+++ b/xlators/mgmt/glusterd/src/glusterd-svc-helper.c
256ebe
@@ -411,9 +411,14 @@ __gf_find_compatible_svc(gd_node_type daemon)
256ebe
     conf = THIS->private;
256ebe
     GF_VALIDATE_OR_GOTO("glusterd", conf, out);
256ebe
 
256ebe
-    if (daemon == GD_NODE_SHD) {
256ebe
-        svc_procs = &conf->shd_procs;
256ebe
-        if (!svc_procs)
256ebe
+    switch (daemon) {
256ebe
+        case GD_NODE_SHD: {
256ebe
+            svc_procs = &conf->shd_procs;
256ebe
+            if (!svc_procs)
256ebe
+                goto out;
256ebe
+        } break;
256ebe
+        default:
256ebe
+            /* Add support for other client daemons here */
256ebe
             goto out;
256ebe
     }
256ebe
 
256ebe
@@ -540,11 +545,16 @@ __gf_find_compatible_svc_from_pid(gd_node_type daemon, pid_t pid)
256ebe
     if (!conf)
256ebe
         return NULL;
256ebe
 
256ebe
-    if (daemon == GD_NODE_SHD) {
256ebe
-        svc_procs = &conf->shd_procs;
256ebe
-        if (!svc_proc)
256ebe
+    switch (daemon) {
256ebe
+        case GD_NODE_SHD: {
256ebe
+            svc_procs = &conf->shd_procs;
256ebe
+            if (!svc_procs)
256ebe
+                return NULL;
256ebe
+        } break;
256ebe
+        default:
256ebe
+            /* Add support for other client daemons here */
256ebe
             return NULL;
256ebe
-    } /* Can be moved to switch when mux is implemented for other daemon; */
256ebe
+    }
256ebe
 
256ebe
     cds_list_for_each_entry(svc_proc, svc_procs, svc_proc_list)
256ebe
     {
256ebe
-- 
256ebe
1.8.3.1
256ebe