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