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/marker/src/marker-quota-helper.c | 6 +++++- xlators/features/marker/src/marker-quota.c | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'xlators/features/marker/src') diff --git a/xlators/features/marker/src/marker-quota-helper.c b/xlators/features/marker/src/marker-quota-helper.c index e3e218f8af5..92d4094e7f8 100644 --- a/xlators/features/marker/src/marker-quota-helper.c +++ b/xlators/features/marker/src/marker-quota-helper.c @@ -297,7 +297,11 @@ mq_dict_set_contribution (xlator_t *this, dict_t *dict, loc_t *loc, goto out; if (contri_key) - strncpy (contri_key, key, QUOTA_KEY_MAX); + if (snprintf(contri_key, QUOTA_KEY_MAX, "%s", key) + >= QUOTA_KEY_MAX) { + ret = -1; + goto out; + } out: if (ret < 0) diff --git a/xlators/features/marker/src/marker-quota.c b/xlators/features/marker/src/marker-quota.c index f6a0df810bc..620201d616e 100644 --- a/xlators/features/marker/src/marker-quota.c +++ b/xlators/features/marker/src/marker-quota.c @@ -2169,7 +2169,11 @@ mq_req_xattr (xlator_t *this, loc_t *loc, dict_t *dict, if (ret < 0) goto out; if (size_key) - strncpy (size_key, key, QUOTA_KEY_MAX); + if (snprintf (size_key, QUOTA_KEY_MAX, "%s", key) + >= QUOTA_KEY_MAX) { + ret = -1; + goto out; + } ret = dict_set_uint64 (dict, key, 0); if (ret < 0) -- cgit