diff options
author | Yaniv Kaul <ykaul@redhat.com> | 2018-08-21 20:39:16 +0300 |
---|---|---|
committer | Amar Tumballi <amarts@redhat.com> | 2018-09-07 03:39:50 +0000 |
commit | ce17a3a66dd15f09d1bf5404f7f3dee860ca6f8c (patch) | |
tree | 41b7b2baafd869b611f4c741df7cd262e17a3bb0 /xlators/cluster/stripe | |
parent | 21e78061a24a094067fb267b77c4ffaae7e762f3 (diff) |
multiple xlators: strncpy()->sprintf(), reduce strlen()'s
xlators/cluster/afr/src/afr-common.c
xlators/cluster/dht/src/dht-common.c
xlators/cluster/dht/src/dht-rebalance.c
xlators/cluster/stripe/src/stripe-helpers.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().
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: Icdf79dd3d9f9ff120e4720ff2b8bd016df575c38
updates: bz#1193929
Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
Diffstat (limited to 'xlators/cluster/stripe')
-rw-r--r-- | xlators/cluster/stripe/src/stripe-helpers.c | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/xlators/cluster/stripe/src/stripe-helpers.c b/xlators/cluster/stripe/src/stripe-helpers.c index 71ab608118f..59dfdfad979 100644 --- a/xlators/cluster/stripe/src/stripe-helpers.c +++ b/xlators/cluster/stripe/src/stripe-helpers.c @@ -253,6 +253,7 @@ stripe_fill_pathinfo_xattr (xlator_t *this, stripe_local_t *local, int ret = -1; int32_t padding = 0; int32_t tlen = 0; + int len = 0; char stripe_size_str[20] = {0,}; char *pathinfo_serz = NULL; @@ -261,12 +262,13 @@ stripe_fill_pathinfo_xattr (xlator_t *this, stripe_local_t *local, goto out; } - (void) snprintf (stripe_size_str, sizeof (stripe_size_str), "%"PRId64, + len = snprintf (stripe_size_str, sizeof (stripe_size_str), "%"PRId64, (long long) (local->fctx) ? local->fctx->stripe_size : 0); - + if (len < 0 || len >= sizeof (stripe_size_str)) + goto out; /* extra bytes for decorations (brackets and <>'s) */ - padding = strlen (this->name) + strlen (STRIPE_PATHINFO_HEADER) - + strlen (stripe_size_str) + 7; + padding = strlen (this->name) + SLEN (STRIPE_PATHINFO_HEADER) + + len + 7; local->xattr_total_len += (padding + 2); pathinfo_serz = GF_MALLOC (local->xattr_total_len, |