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