summaryrefslogtreecommitdiffstats
path: root/xlators/cluster/afr/src/afr-common.c
diff options
context:
space:
mode:
authorYaniv Kaul <ykaul@redhat.com>2018-08-21 20:39:16 +0300
committerAmar Tumballi <amarts@redhat.com>2018-09-07 03:39:50 +0000
commitce17a3a66dd15f09d1bf5404f7f3dee860ca6f8c (patch)
tree41b7b2baafd869b611f4c741df7cd262e17a3bb0 /xlators/cluster/afr/src/afr-common.c
parent21e78061a24a094067fb267b77c4ffaae7e762f3 (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/afr/src/afr-common.c')
-rw-r--r--xlators/cluster/afr/src/afr-common.c25
1 files changed, 11 insertions, 14 deletions
diff --git a/xlators/cluster/afr/src/afr-common.c b/xlators/cluster/afr/src/afr-common.c
index 641485b1ed0..3a32ebc31a4 100644
--- a/xlators/cluster/afr/src/afr-common.c
+++ b/xlators/cluster/afr/src/afr-common.c
@@ -2178,7 +2178,7 @@ afr_is_xattr_ignorable (char *key)
{
int i = 0;
- if (!strncmp (key, AFR_XATTR_PREFIX, strlen(AFR_XATTR_PREFIX)))
+ if (!strncmp (key, AFR_XATTR_PREFIX, SLEN (AFR_XATTR_PREFIX)))
return _gf_true;
for (i = 0; afr_ignore_xattrs[i]; i++) {
if (!strcmp (key, afr_ignore_xattrs[i]))
@@ -6212,7 +6212,6 @@ afr_get_heal_info (call_frame_t *frame, xlator_t *this, loc_t *loc)
dict_t *dict = NULL;
int ret = -1;
int op_errno = 0;
- int size = 0;
inode_t *inode = NULL;
char *substr = NULL;
char *status = NULL;
@@ -6229,21 +6228,18 @@ afr_get_heal_info (call_frame_t *frame, xlator_t *this, loc_t *loc)
}
if (pending) {
- size = strlen ("-pending") + 1;
gf_asprintf (&substr, "-pending");
if (!substr)
goto out;
}
if (ret == -EIO) {
- size += strlen ("split-brain") + 1;
ret = gf_asprintf (&status, "split-brain%s",
substr? substr : "");
if (ret < 0)
goto out;
dict = afr_set_heal_info (status);
} else if (ret == -EAGAIN) {
- size += strlen ("possibly-healing") + 1;
ret = gf_asprintf (&status, "possibly-healing%s",
substr? substr : "");
if (ret < 0)
@@ -6258,7 +6254,6 @@ afr_get_heal_info (call_frame_t *frame, xlator_t *this, loc_t *loc)
!metadata_selfheal) {
dict = afr_set_heal_info ("no-heal");
} else {
- size += strlen ("heal") + 1;
ret = gf_asprintf (&status, "heal%s",
substr? substr : "");
if (ret < 0)
@@ -6275,7 +6270,6 @@ afr_get_heal_info (call_frame_t *frame, xlator_t *this, loc_t *loc)
*/
if (data_selfheal || entry_selfheal ||
metadata_selfheal) {
- size += strlen ("heal") + 1;
ret = gf_asprintf (&status, "heal%s",
substr? substr : "");
if (ret < 0)
@@ -6409,13 +6403,13 @@ afr_get_split_brain_status (void *opaque)
}
/* Calculation for string length :
- * (child_count X length of child-name) + strlen (" Choices :")
+ * (child_count X length of child-name) + SLEN (" Choices :")
* child-name consists of :
* a) 251 = max characters for volname according to GD_VOLUME_NAME_MAX
* b) strlen ("-client-00,") assuming 16 replicas
*/
- choices = alloca0 (priv->child_count * (251 + sizeof("-client-00,")) +
- sizeof(" Choices:"));
+ choices = alloca0 (priv->child_count * (256 + SLEN ("-client-00,")) +
+ SLEN (" Choices:"));
ret = afr_is_split_brain (frame, this, inode, loc->gfid, &d_spb,
&m_spb);
@@ -6717,6 +6711,7 @@ afr_serialize_xattrs_with_delimiter (call_frame_t *frame, xlator_t *this,
char *xattr = NULL;
int i = 0;
int len = 0;
+ size_t str_len = 0;
int ret = -1;
priv = this->private;
@@ -6724,8 +6719,9 @@ afr_serialize_xattrs_with_delimiter (call_frame_t *frame, xlator_t *this,
for (i = 0; i < priv->child_count; i++) {
if (!local->replies[i].valid || local->replies[i].op_ret) {
- buf = strncat (buf, default_str, strlen (default_str));
- len += strlen (default_str);
+ str_len = strlen (default_str);
+ buf = strncat (buf, default_str, str_len);
+ len += str_len;
buf[len++] = delimiter;
buf[len] = '\0';
} else {
@@ -6738,8 +6734,9 @@ afr_serialize_xattrs_with_delimiter (call_frame_t *frame, xlator_t *this,
"%d", i);
goto out;
}
- buf = strncat (buf, xattr, strlen (xattr));
- len += strlen (xattr);
+ str_len = strlen (xattr);
+ buf = strncat (buf, xattr, str_len);
+ len += str_len;
buf[len++] = delimiter;
buf[len] = '\0';
}