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-sm.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'xlators/mgmt/glusterd/src/glusterd-sm.c') diff --git a/xlators/mgmt/glusterd/src/glusterd-sm.c b/xlators/mgmt/glusterd/src/glusterd-sm.c index 0a52cc92a58..59a2f635106 100644 --- a/xlators/mgmt/glusterd/src/glusterd-sm.c +++ b/xlators/mgmt/glusterd/src/glusterd-sm.c @@ -1017,9 +1017,14 @@ glusterd_ac_handle_friend_add_req (glusterd_friend_sm_event_t *event, void *ctx) if (ret || !hostname) { gf_msg_debug (this->name, 0, "Unable to fetch local hostname from peer"); - } else - strncpy (local_node_hostname, hostname, - sizeof(local_node_hostname)); + } else if (snprintf (local_node_hostname, + sizeof (local_node_hostname), "%s", hostname) >= + sizeof (local_node_hostname)) { + gf_msg_debug (this->name, 0, + "local_node_hostname truncated"); + ret = -1; + goto out; + } glusterd_friend_sm_inject_event (new_event); new_event = NULL; -- cgit