From 052849983e51a061d7fb2c3ffd74fa78bb257084 Mon Sep 17 00:00:00 2001 From: Yaniv Kaul Date: Tue, 21 Aug 2018 20:43:07 +0300 Subject: Various files: strncpy()->sprintf(), reduce strlen()'s 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(). Check for truncated output where applicable. 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: I54e80d4f4a80e98d3775e376efe05c51af0b29eb updates: bz#1193929 Signed-off-by: Yaniv Kaul --- xlators/features/locks/src/posix.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'xlators/features/locks/src/posix.c') diff --git a/xlators/features/locks/src/posix.c b/xlators/features/locks/src/posix.c index c4ca71cfd7f..22d5990275d 100644 --- a/xlators/features/locks/src/posix.c +++ b/xlators/features/locks/src/posix.c @@ -1062,7 +1062,7 @@ pl_getxattr (call_frame_t *frame, xlator_t *this, loc_t *loc, if (!name) goto usual; - if (strncmp (name, GF_XATTR_CLRLK_CMD, strlen (GF_XATTR_CLRLK_CMD))) + if (strncmp (name, GF_XATTR_CLRLK_CMD, SLEN (GF_XATTR_CLRLK_CMD))) goto usual; if (clrlk_parse_args (name, &args)) { @@ -1136,7 +1136,10 @@ pl_getxattr (call_frame_t *frame, xlator_t *this, loc_t *loc, goto out; } - strncpy (key, name, strlen (name)); + if (snprintf (key, sizeof (key), "%s", name) >= sizeof (key)) { + op_ret = -1; + goto out; + } if (dict_set_dynstr (dict, key, lk_summary)) { op_ret = -1; op_errno = ENOMEM; -- cgit