74096c
From 7fe500a03d42dba6082c28ef7284c950c44fbfa3 Mon Sep 17 00:00:00 2001
74096c
From: Xavi Hernandez <xhernandez@redhat.com>
74096c
Date: Wed, 22 May 2019 17:46:19 +0200
74096c
Subject: [PATCH 430/449] Fix some "Null pointer dereference" coverity issues
74096c
74096c
This patch fixes the following CID's:
74096c
74096c
  * 1124829
74096c
  * 1274075
74096c
  * 1274083
74096c
  * 1274128
74096c
  * 1274135
74096c
  * 1274141
74096c
  * 1274143
74096c
  * 1274197
74096c
  * 1274205
74096c
  * 1274210
74096c
  * 1274211
74096c
  * 1288801
74096c
  * 1398629
74096c
74096c
Backport of:
74096c
> Upstream-patch-link: https://review.gluster.org/22767
74096c
> Change-Id: Ia7c86cfab3245b20777ffa296e1a59748040f558
74096c
> Updates: bz#789278
74096c
> Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
74096c
74096c
BUG: 1787310
74096c
Change-Id: Ia7c86cfab3245b20777ffa296e1a59748040f558
74096c
Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
74096c
Reviewed-on: https://code.engineering.redhat.com/gerrit/202616
74096c
Tested-by: RHGS Build Bot <nigelb@redhat.com>
74096c
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
74096c
---
74096c
 cli/src/cli-cmd-system.c                     |  2 +-
74096c
 cli/src/cli-xml-output.c                     |  2 +-
74096c
 glusterfsd/src/glusterfsd.c                  | 24 +++++++++++++-----------
74096c
 libglusterfs/src/inode.c                     |  3 +++
74096c
 rpc/rpc-lib/src/rpcsvc.c                     |  4 ++++
74096c
 xlators/cluster/dht/src/dht-shared.c         |  4 ++++
74096c
 xlators/cluster/dht/src/switch.c             |  9 +++++++++
74096c
 xlators/features/trash/src/trash.c           |  2 +-
74096c
 xlators/mgmt/glusterd/src/glusterd-geo-rep.c |  7 +++++--
74096c
 xlators/nfs/server/src/mount3.c              |  6 ++++++
74096c
 xlators/protocol/client/src/client.c         |  7 ++++++-
74096c
 xlators/storage/posix/src/posix-helpers.c    |  3 +++
74096c
 12 files changed, 56 insertions(+), 17 deletions(-)
74096c
74096c
diff --git a/cli/src/cli-cmd-system.c b/cli/src/cli-cmd-system.c
74096c
index 8cd1542..cb3a9ea 100644
74096c
--- a/cli/src/cli-cmd-system.c
74096c
+++ b/cli/src/cli-cmd-system.c
74096c
@@ -446,7 +446,7 @@ cli_cmd_sys_exec_cbk(struct cli_state *state, struct cli_cmd_word *word,
74096c
     dict_t *dict = NULL;
74096c
     cli_local_t *local = NULL;
74096c
 
74096c
-    if (wordcount < 3) {
74096c
+    if ((wordcount < 3) || (words[2] == NULL)) {
74096c
         cli_usage_out(word->pattern);
74096c
         goto out;
74096c
     }
74096c
diff --git a/cli/src/cli-xml-output.c b/cli/src/cli-xml-output.c
74096c
index 006e2fb..903997c 100644
74096c
--- a/cli/src/cli-xml-output.c
74096c
+++ b/cli/src/cli-xml-output.c
74096c
@@ -64,7 +64,7 @@ cli_begin_xml_output(xmlTextWriterPtr *writer, xmlDocPtr *doc)
74096c
     int ret = -1;
74096c
 
74096c
     *writer = xmlNewTextWriterDoc(doc, 0);
74096c
-    if (writer == NULL) {
74096c
+    if (*writer == NULL) {
74096c
         ret = -1;
74096c
         goto out;
74096c
     }
74096c
diff --git a/glusterfsd/src/glusterfsd.c b/glusterfsd/src/glusterfsd.c
74096c
index 974fb88..9821180 100644
74096c
--- a/glusterfsd/src/glusterfsd.c
74096c
+++ b/glusterfsd/src/glusterfsd.c
74096c
@@ -1235,19 +1235,21 @@ parse_opts(int key, char *arg, struct argp_state *state)
74096c
         case ARGP_BRICK_PORT_KEY:
74096c
             n = 0;
74096c
 
74096c
-            port_str = strtok_r(arg, ",", &tmp_str);
74096c
-            if (gf_string2uint_base10(port_str, &n) == 0) {
74096c
-                cmd_args->brick_port = n;
74096c
-                port_str = strtok_r(NULL, ",", &tmp_str);
74096c
-                if (port_str) {
74096c
-                    if (gf_string2uint_base10(port_str, &n) == 0) {
74096c
-                        cmd_args->brick_port2 = n;
74096c
-                        break;
74096c
+            if (arg != NULL) {
74096c
+                port_str = strtok_r(arg, ",", &tmp_str);
74096c
+                if (gf_string2uint_base10(port_str, &n) == 0) {
74096c
+                    cmd_args->brick_port = n;
74096c
+                    port_str = strtok_r(NULL, ",", &tmp_str);
74096c
+                    if (port_str) {
74096c
+                        if (gf_string2uint_base10(port_str, &n) == 0) {
74096c
+                            cmd_args->brick_port2 = n;
74096c
+                            break;
74096c
+                        }
74096c
+                        argp_failure(state, -1, 0,
74096c
+                                     "wrong brick (listen) port %s", arg);
74096c
                     }
74096c
-                    argp_failure(state, -1, 0, "wrong brick (listen) port %s",
74096c
-                                 arg);
74096c
+                    break;
74096c
                 }
74096c
-                break;
74096c
             }
74096c
 
74096c
             argp_failure(state, -1, 0, "unknown brick (listen) port %s", arg);
74096c
diff --git a/libglusterfs/src/inode.c b/libglusterfs/src/inode.c
74096c
index 9dbb25b..4c3c546 100644
74096c
--- a/libglusterfs/src/inode.c
74096c
+++ b/libglusterfs/src/inode.c
74096c
@@ -899,6 +899,9 @@ inode_resolve(inode_table_t *table, char *path)
74096c
 
74096c
     parent = inode_ref(table->root);
74096c
     str = tmp = gf_strdup(path);
74096c
+    if (str == NULL) {
74096c
+        goto out;
74096c
+    }
74096c
 
74096c
     while (1) {
74096c
         bname = strtok_r(str, "/", &saveptr);
74096c
diff --git a/rpc/rpc-lib/src/rpcsvc.c b/rpc/rpc-lib/src/rpcsvc.c
74096c
index 5a35139..b058932 100644
74096c
--- a/rpc/rpc-lib/src/rpcsvc.c
74096c
+++ b/rpc/rpc-lib/src/rpcsvc.c
74096c
@@ -2874,6 +2874,10 @@ rpcsvc_transport_peer_check_search(dict_t *options, char *pattern, char *ip,
74096c
     }
74096c
 
74096c
     dup_addrstr = gf_strdup(addrstr);
74096c
+    if (dup_addrstr == NULL) {
74096c
+        ret = -1;
74096c
+        goto err;
74096c
+    }
74096c
     addrtok = strtok_r(dup_addrstr, ",", &svptr);
74096c
     while (addrtok) {
74096c
         /* CASEFOLD not present on Solaris */
74096c
diff --git a/xlators/cluster/dht/src/dht-shared.c b/xlators/cluster/dht/src/dht-shared.c
74096c
index ea4b7c6..58e3339 100644
74096c
--- a/xlators/cluster/dht/src/dht-shared.c
74096c
+++ b/xlators/cluster/dht/src/dht-shared.c
74096c
@@ -278,6 +278,10 @@ dht_parse_decommissioned_bricks(xlator_t *this, dht_conf_t *conf,
74096c
         goto out;
74096c
 
74096c
     dup_brick = gf_strdup(bricks);
74096c
+    if (dup_brick == NULL) {
74096c
+        goto out;
74096c
+    }
74096c
+
74096c
     node = strtok_r(dup_brick, ",", &tmpstr);
74096c
     while (node) {
74096c
         for (i = 0; i < conf->subvolume_cnt; i++) {
74096c
diff --git a/xlators/cluster/dht/src/switch.c b/xlators/cluster/dht/src/switch.c
74096c
index a782fcd..207d109 100644
74096c
--- a/xlators/cluster/dht/src/switch.c
74096c
+++ b/xlators/cluster/dht/src/switch.c
74096c
@@ -610,9 +610,15 @@ set_switch_pattern(xlator_t *this, dht_conf_t *conf, const char *pattern_str)
74096c
     /* Get the pattern for considering switch case.
74096c
        "option block-size *avi:10MB" etc */
74096c
     option_string = gf_strdup(pattern_str);
74096c
+    if (option_string == NULL) {
74096c
+        goto err;
74096c
+    }
74096c
     switch_str = strtok_r(option_string, ";", &tmp_str);
74096c
     while (switch_str) {
74096c
         dup_str = gf_strdup(switch_str);
74096c
+        if (dup_str == NULL) {
74096c
+            goto err;
74096c
+        }
74096c
         switch_opt = GF_CALLOC(1, sizeof(struct switch_struct),
74096c
                                gf_switch_mt_switch_struct);
74096c
         if (!switch_opt) {
74096c
@@ -647,6 +653,9 @@ set_switch_pattern(xlator_t *this, dht_conf_t *conf, const char *pattern_str)
74096c
 
74096c
         if (childs) {
74096c
             dup_childs = gf_strdup(childs);
74096c
+            if (dup_childs == NULL) {
74096c
+                goto err;
74096c
+            }
74096c
             child = strtok_r(dup_childs, ",", &tmp);
74096c
             while (child) {
74096c
                 if (gf_switch_valid_child(this, child)) {
74096c
diff --git a/xlators/features/trash/src/trash.c b/xlators/features/trash/src/trash.c
74096c
index d668436..f96ed73 100644
74096c
--- a/xlators/features/trash/src/trash.c
74096c
+++ b/xlators/features/trash/src/trash.c
74096c
@@ -170,7 +170,7 @@ store_eliminate_path(char *str, trash_elim_path **eliminate)
74096c
     int ret = 0;
74096c
     char *strtokptr = NULL;
74096c
 
74096c
-    if (eliminate == NULL) {
74096c
+    if ((str == NULL) || (eliminate == NULL)) {
74096c
         ret = EINVAL;
74096c
         goto out;
74096c
     }
74096c
diff --git a/xlators/mgmt/glusterd/src/glusterd-geo-rep.c b/xlators/mgmt/glusterd/src/glusterd-geo-rep.c
74096c
index 0f40bea..85c06c1 100644
74096c
--- a/xlators/mgmt/glusterd/src/glusterd-geo-rep.c
74096c
+++ b/xlators/mgmt/glusterd/src/glusterd-geo-rep.c
74096c
@@ -5981,7 +5981,7 @@ glusterd_get_slave_info(char *slave, char **slave_url, char **hostname,
74096c
     GF_ASSERT(this);
74096c
 
74096c
     ret = glusterd_urltransform_single(slave, "normalize", &linearr);
74096c
-    if (ret == -1) {
74096c
+    if ((ret == -1) || (linearr[0] == NULL)) {
74096c
         ret = snprintf(errmsg, sizeof(errmsg) - 1, "Invalid Url: %s", slave);
74096c
         errmsg[ret] = '\0';
74096c
         *op_errstr = gf_strdup(errmsg);
74096c
@@ -5992,7 +5992,10 @@ glusterd_get_slave_info(char *slave, char **slave_url, char **hostname,
74096c
 
74096c
     tmp = strtok_r(linearr[0], "/", &save_ptr);
74096c
     tmp = strtok_r(NULL, "/", &save_ptr);
74096c
-    slave = strtok_r(tmp, ":", &save_ptr);
74096c
+    slave = NULL;
74096c
+    if (tmp != NULL) {
74096c
+        slave = strtok_r(tmp, ":", &save_ptr);
74096c
+    }
74096c
     if (slave) {
74096c
         ret = glusterd_geo_rep_parse_slave(slave, hostname, op_errstr);
74096c
         if (ret) {
74096c
diff --git a/xlators/nfs/server/src/mount3.c b/xlators/nfs/server/src/mount3.c
74096c
index 396809c..734453c 100644
74096c
--- a/xlators/nfs/server/src/mount3.c
74096c
+++ b/xlators/nfs/server/src/mount3.c
74096c
@@ -3205,6 +3205,12 @@ mnt3_export_parse_auth_param(struct mnt3_export *exp, char *exportpath)
74096c
     struct host_auth_spec *host = NULL;
74096c
     int ret = 0;
74096c
 
74096c
+    if (exportpath == NULL) {
74096c
+        gf_msg(GF_MNT, GF_LOG_ERROR, EINVAL, NFS_MSG_PARSE_HOSTSPEC_FAIL,
74096c
+               "Export path is NULL");
74096c
+        return -1;
74096c
+    }
74096c
+
74096c
     /* Using exportpath directly in strtok_r because we want
74096c
      * to strip off AUTH parameter from exportpath. */
74096c
     token = strtok_r(exportpath, "(", &savPtr);
74096c
diff --git a/xlators/protocol/client/src/client.c b/xlators/protocol/client/src/client.c
74096c
index e156d4d..ed855ca 100644
74096c
--- a/xlators/protocol/client/src/client.c
74096c
+++ b/xlators/protocol/client/src/client.c
74096c
@@ -1222,9 +1222,12 @@ client_set_remote_options(char *value, xlator_t *this)
74096c
     char *remote_port_str = NULL;
74096c
     char *tmp = NULL;
74096c
     int remote_port = 0;
74096c
-    int ret = 0;
74096c
+    int ret = -1;
74096c
 
74096c
     dup_value = gf_strdup(value);
74096c
+    if (dup_value == NULL) {
74096c
+        goto out;
74096c
+    }
74096c
     host = strtok_r(dup_value, ":", &tmp);
74096c
     subvol = strtok_r(NULL, ":", &tmp);
74096c
     remote_port_str = strtok_r(NULL, ":", &tmp);
74096c
@@ -1238,6 +1241,7 @@ client_set_remote_options(char *value, xlator_t *this)
74096c
         if (ret) {
74096c
             gf_msg(this->name, GF_LOG_WARNING, 0, PC_MSG_DICT_SET_FAILED,
74096c
                    "failed to set remote-host with %s", host);
74096c
+            GF_FREE(host_dup);
74096c
             goto out;
74096c
         }
74096c
     }
74096c
@@ -1252,6 +1256,7 @@ client_set_remote_options(char *value, xlator_t *this)
74096c
         if (ret) {
74096c
             gf_msg(this->name, GF_LOG_WARNING, 0, PC_MSG_DICT_SET_FAILED,
74096c
                    "failed to set remote-host with %s", host);
74096c
+            GF_FREE(subvol_dup);
74096c
             goto out;
74096c
         }
74096c
     }
74096c
diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c
74096c
index 949c799..2336add 100644
74096c
--- a/xlators/storage/posix/src/posix-helpers.c
74096c
+++ b/xlators/storage/posix/src/posix-helpers.c
74096c
@@ -390,6 +390,9 @@ _posix_get_marker_quota_contributions(posix_xattr_filler_t *filler, char *key)
74096c
     int i = 0, ret = 0;
74096c
 
74096c
     tmp_key = ptr = gf_strdup(key);
74096c
+    if (tmp_key == NULL) {
74096c
+        return -1;
74096c
+    }
74096c
     for (i = 0; i < 4; i++) {
74096c
         token = strtok_r(tmp_key, ".", &saveptr);
74096c
         tmp_key = NULL;
74096c
-- 
74096c
1.8.3.1
74096c