From a880d6f6aa7a2979df8aa32b58f716ef0c578d3f Mon Sep 17 00:00:00 2001 From: Yaniv Kaul Date: Tue, 21 Aug 2018 20:41:20 +0300 Subject: multiple xlators (mgmt): strncpy()->sprintf(), reduce strlen()'s xlators/mgmt/glusterd/src/glusterd-geo-rep.c xlators/mgmt/glusterd/src/glusterd-handshake.c xlators/mgmt/glusterd/src/glusterd-sm.c xlators/mgmt/glusterd/src/glusterd-store.c xlators/mgmt/glusterd/src/glusterd-utils.c xlators/mgmt/glusterd/src/glusterd-volgen.c xlators/mgmt/glusterd/src/glusterd-volume-ops.c xlators/mgmt/glusterd/src/glusterd.c strncpy may not be very efficient for short strings copied into a large buffer: If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure that a total of n bytes are written. Instead, use snprintf(). Try to ensure output is not truncated. Also: - save the result of strlen() and re-use it when possible. - move from strlen to SLEN (sizeof() ) for const strings. Compile-tested only! Change-Id: Ib5d001857236f43e41c4a51b5f48e1a33110aaeb updates: bz#1193929 Signed-off-by: Yaniv Kaul --- xlators/mgmt/glusterd/src/glusterd-volume-ops.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'xlators/mgmt/glusterd/src/glusterd-volume-ops.c') diff --git a/xlators/mgmt/glusterd/src/glusterd-volume-ops.c b/xlators/mgmt/glusterd/src/glusterd-volume-ops.c index 030006b5459..14638625d47 100644 --- a/xlators/mgmt/glusterd/src/glusterd-volume-ops.c +++ b/xlators/mgmt/glusterd/src/glusterd-volume-ops.c @@ -1148,7 +1148,7 @@ glusterd_is_valid_vg (glusterd_brickinfo_t *brick, int check_tag, char *msg) dm_list_iterate_items (strl, taglist) { if (!strncmp(strl->str, GF_XATTR_VOL_ID_KEY, - strlen (GF_XATTR_VOL_ID_KEY))) { + SLEN (GF_XATTR_VOL_ID_KEY))) { sprintf (msg, "VG %s is already part of" " a brick", vg_name); retval = -1; @@ -2226,7 +2226,12 @@ glusterd_op_create_volume (dict_t *dict, char **op_errstr) goto out; } - strncpy (volinfo->volname, volname, sizeof(volinfo->volname) - 1); + if (snprintf (volinfo->volname, sizeof (volinfo->volname), + "%s", volname) >= sizeof (volinfo->volname)) { + ret = -1; + goto out; + } + GF_ASSERT (volinfo->volname); ret = dict_get_int32 (dict, "type", &volinfo->type); @@ -2477,8 +2482,9 @@ glusterd_op_create_volume (dict_t *dict, char **op_errstr) "%s not present", key); goto out; } - strncpy (brickinfo->mount_dir, brick_mount_dir, - sizeof(brickinfo->mount_dir)); + snprintf (brickinfo->mount_dir, + sizeof (brickinfo->mount_dir), "%s", + brick_mount_dir); } if (!gf_uuid_compare (brickinfo->uuid, MY_UUID)) { @@ -2696,8 +2702,13 @@ glusterd_op_start_volume (dict_t *dict, char **op_errstr) "%s not present", key); goto out; } - strncpy (brickinfo->mount_dir, brick_mount_dir, - sizeof(brickinfo->mount_dir)); + if (snprintf (brickinfo->mount_dir, + sizeof (brickinfo->mount_dir), + "%s", brick_mount_dir) + >= sizeof (brickinfo->mount_dir)) { + ret = -1; + goto out; + } } } } -- cgit