From 7772315bd7d82d5f06f008dfe767f1e597a41b23 Mon Sep 17 00:00:00 2001 From: Yaniv Kaul Date: Tue, 21 Aug 2018 20:42:53 +0300 Subject: multiple xlators (storage/posix): strncpy()->sprintf(), reduce strlen()'s xlators/storage/posix/src/posix-gfid-path.c xlators/storage/posix/src/posix-handle.c xlators/storage/posix/src/posix-helpers.c xlators/storage/posix/src/posix-inode-fd-ops.c xlators/storage/posix/src/posix.h 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(). 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: I056939f111a4ec6bc8ebd539ebcaf9eb67da6c95 updates: bz#1193929 Signed-off-by: Yaniv Kaul --- xlators/storage/posix/src/posix-helpers.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'xlators/storage/posix/src/posix-helpers.c') diff --git a/xlators/storage/posix/src/posix-helpers.c b/xlators/storage/posix/src/posix-helpers.c index 99be6366e11..b4d433e369d 100644 --- a/xlators/storage/posix/src/posix-helpers.c +++ b/xlators/storage/posix/src/posix-helpers.c @@ -329,6 +329,7 @@ _posix_get_marker_all_contributions (posix_xattr_filler_t *filler) { ssize_t size = -1, remaining_size = -1, list_offset = 0; int ret = -1; + int len; char *list = NULL, key[4096] = {0, }; if (filler->real_path) @@ -384,9 +385,9 @@ _posix_get_marker_all_contributions (posix_xattr_filler_t *filler) if (fnmatch (marker_contri_key, key, 0) == 0) { ret = _posix_xattr_get_set_from_backend (filler, key); } - - remaining_size -= strlen (key) + 1; - list_offset += strlen (key) + 1; + len = strlen (key); + remaining_size -= (len + 1); + list_offset += (len + 1); } ret = 0; @@ -408,7 +409,7 @@ _posix_get_marker_quota_contributions (posix_xattr_filler_t *filler, char *key) tmp_key = NULL; } - if (strncmp (token, "contri", strlen ("contri")) == 0) { + if (strncmp (token, "contri", SLEN ("contri")) == 0) { ret = _posix_get_marker_all_contributions (filler); } else { ret = _posix_xattr_get_set_from_backend (filler, key); @@ -436,6 +437,7 @@ _posix_xattr_get_set (dict_t *xattr_req, char *key, data_t *data, { posix_xattr_filler_t *filler = xattrargs; int ret = -1; + int len; char *databuf = NULL; int _fd = -1; ssize_t req_size = 0; @@ -577,8 +579,9 @@ _posix_xattr_get_set (dict_t *xattr_req, char *key, data_t *data, if (fnmatch (key, xattr, 0) == 0) ret = _posix_xattr_get_set_from_backend (filler, xattr); - remaining_size -= strlen (xattr) + 1; - list_offset += strlen (xattr) + 1; + len = strlen (xattr); + remaining_size -= (len + 1); + list_offset += (len + 1); } } out: @@ -869,6 +872,7 @@ _handle_list_xattr (dict_t *xattr_req, const char *real_path, int fdnum, int32_t list_offset = 0; ssize_t remaining_size = 0; char *key = NULL; + int len; list_offset = 0; remaining_size = filler->list_size; @@ -892,8 +896,9 @@ _handle_list_xattr (dict_t *xattr_req, const char *real_path, int fdnum, (void) _posix_xattr_get_set_from_backend (filler, key); next: - remaining_size -= strlen (key) + 1; - list_offset += strlen (key) + 1; + len = strlen (key); + remaining_size -= (len + 1); + list_offset += (len + 1); } /* while (remaining_size > 0) */ return; @@ -2739,7 +2744,7 @@ posix_resolve_dirgfid_to_path (const uuid_t dirgfid, const char *brick_path, goto out; } - strncpy (pre_dir_name, result, sizeof(pre_dir_name)); + snprintf (pre_dir_name, sizeof (pre_dir_name), "%s", result); gf_uuid_parse (pgfidstr, tmp_gfid); gf_uuid_copy (pargfid, tmp_gfid); -- cgit