summaryrefslogtreecommitdiffstats
path: root/xlators/mgmt/glusterd/src/glusterd-utils.c
diff options
context:
space:
mode:
authorKaleb S. KEITHLE <kkeithle@redhat.com>2018-11-09 09:45:05 -0500
committerAmar Tumballi <amarts@redhat.com>2018-11-15 05:06:59 +0000
commit4a4ba1f2eb0be2da9e88560246730af87788295f (patch)
treebc0a3bc043e012efc60dc58e131abb47c50ce979 /xlators/mgmt/glusterd/src/glusterd-utils.c
parent76906af9d70fc784de728a70e3dbda62dece5e10 (diff)
core: fix strncpy warnings
Since gcc-8.2.x (fedora-28 or so) gcc has been emitting warnings about buggy use of strncpy. Most uses that gcc warns about in our sources are exactly backwards; the 'limit' or len is the strlen/size of the _source param_, giving exactly zero protection against overruns. (Which was, after all, one of the points of using strncpy in the first place.) IOW, many warnings are about uses that look approximately like this: ... char dest[8]; char src[] = "this is a string longer than eight chars"; ... strncpy (dest, src, sizeof(src)); /* boom */ ... The len/limit should be sizeof(dest). Note: the above example has a definite over-run. In our source the overrun is typically only theoretical (but possibly exploitable.) Also strncpy doesn't null-terminate on truncation; snprintf does; prefer snprintf over strncpy. Mildly surprising that coverity doesn't warn/isn't warning about this. Change-Id: I022d5c6346a751e181ad44d9a099531c1172626e updates: bz#1193929 Signed-off-by: Kaleb S. KEITHLE <kkeithle@redhat.com>
Diffstat (limited to 'xlators/mgmt/glusterd/src/glusterd-utils.c')
-rw-r--r--xlators/mgmt/glusterd/src/glusterd-utils.c36
1 files changed, 24 insertions, 12 deletions
diff --git a/xlators/mgmt/glusterd/src/glusterd-utils.c b/xlators/mgmt/glusterd/src/glusterd-utils.c
index e633d6ebc4d..d9e0d6110a0 100644
--- a/xlators/mgmt/glusterd/src/glusterd-utils.c
+++ b/xlators/mgmt/glusterd/src/glusterd-utils.c
@@ -1294,7 +1294,12 @@ glusterd_brickinfo_new_from_brick(char *brick, glusterd_brickinfo_t **brickinfo,
goto out;
}
}
- strncpy(new_brickinfo->real_path, abspath, strlen(abspath));
+ if (strlen(abspath) >= sizeof(new_brickinfo->real_path)) {
+ ret = -1;
+ goto out;
+ }
+ (void)strncpy(new_brickinfo->real_path, abspath,
+ sizeof(new_brickinfo->real_path));
}
*brickinfo = new_brickinfo;
@@ -1366,7 +1371,7 @@ glusterd_is_brickpath_available(uuid_t uuid, char *path)
glusterd_volinfo_t *volinfo = NULL;
glusterd_conf_t *priv = NULL;
gf_boolean_t available = _gf_false;
- char tmp_path[PATH_MAX + 1] = "";
+ char tmp_path[PATH_MAX] = "";
priv = THIS->private;
@@ -1385,7 +1390,7 @@ glusterd_is_brickpath_available(uuid_t uuid, char *path)
goto out;
}
/* When realpath(3) fails, tmp_path is undefined. */
- strncpy(tmp_path, path, PATH_MAX);
+ (void)snprintf(tmp_path, sizeof(tmp_path), "%s", path);
}
cds_list_for_each_entry(volinfo, &priv->volumes, vol_list)
@@ -3762,8 +3767,8 @@ glusterd_import_new_brick(dict_t *peer_data, int32_t vol_count,
}
new_brickinfo->decommissioned = decommissioned;
if (brick_id)
- gf_strncpy(new_brickinfo->brick_id, brick_id,
- sizeof(new_brickinfo->brick_id));
+ (void)snprintf(new_brickinfo->brick_id, sizeof(new_brickinfo->brick_id),
+ "%s", brick_id);
snprintf(key, sizeof(key), "%s%d.brick%d", prefix, vol_count, brick_count);
ret = gd_import_new_brick_snap_details(peer_data, key, new_brickinfo);
@@ -4441,10 +4446,16 @@ glusterd_volinfo_copy_brickinfo(glusterd_volinfo_t *old_volinfo,
ret = -1;
goto out;
}
- strncpy(new_brickinfo->real_path, abspath, strlen(abspath));
+ if (strlen(abspath) >= sizeof(new_brickinfo->real_path)) {
+ ret = -1;
+ goto out;
+ }
+ (void)strncpy(new_brickinfo->real_path, abspath,
+ sizeof(new_brickinfo->real_path));
} else {
- strncpy(new_brickinfo->real_path, old_brickinfo->real_path,
- strlen(old_brickinfo->real_path));
+ (void)strncpy(new_brickinfo->real_path,
+ old_brickinfo->real_path,
+ sizeof(new_brickinfo->real_path));
}
}
}
@@ -5283,7 +5294,7 @@ glusterd_remote_hostname_get(rpcsvc_request_t *req, char *remote_host, int len)
tmp_host = hostname = canon;
}
- strncpy(remote_host, hostname, strlen(hostname));
+ (void)snprintf(remote_host, len, "%s", hostname);
out:
GF_FREE(tmp_host);
@@ -6472,7 +6483,6 @@ _local_gsyncd_start(dict_t *this, char *key, data_t *value, void *data)
char *slave_host = NULL;
char *statefile = NULL;
char buf[1024] = "faulty";
- int uuid_len = 0;
int ret = 0;
int op_ret = 0;
int ret_status = 0;
@@ -6498,9 +6508,8 @@ _local_gsyncd_start(dict_t *this, char *key, data_t *value, void *data)
slave++;
else
return 0;
- uuid_len = (slave - value->data - 1);
- strncpy(uuid_str, (char *)value->data, uuid_len);
+ (void)snprintf(uuid_str, sizeof(uuid_str), "%s", (char *)value->data);
/* Getting Local Brickpaths */
ret = glusterd_get_local_brickpaths(volinfo, &path_list);
@@ -12883,6 +12892,9 @@ glusterd_update_mntopts(char *brick_path, glusterd_brickinfo_t *brickinfo)
ret = -1;
goto out;
}
+ (void)snprintf(brickinfo->mnt_opts, sizeof(brickinfo->mnt_opts), "%s",
+ entry->mnt_opts);
+
gf_strncpy(brickinfo->mnt_opts, entry->mnt_opts,
sizeof(brickinfo->mnt_opts));