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