summaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--xlators/cluster/afr/src/afr-common.c25
-rw-r--r--xlators/cluster/dht/src/dht-common.c22
-rw-r--r--xlators/cluster/dht/src/dht-rebalance.c6
-rw-r--r--xlators/cluster/stripe/src/stripe-helpers.c10
4 files changed, 30 insertions, 33 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';
}
diff --git a/xlators/cluster/dht/src/dht-common.c b/xlators/cluster/dht/src/dht-common.c
index c7c7fbf22ba..69555a22e5e 100644
--- a/xlators/cluster/dht/src/dht-common.c
+++ b/xlators/cluster/dht/src/dht-common.c
@@ -428,7 +428,7 @@ dht_aggregate (dict_t *this, char *key, data_t *value, void *data)
goto out;
} else {
/* compare user xattrs only */
- if (!strncmp (key, "user.", strlen ("user."))) {
+ if (!strncmp (key, "user.", SLEN ("user."))) {
ret = dict_lookup (dst, key, &dict_data);
if (!ret && dict_data && value) {
ret = is_data_equal (dict_data, value);
@@ -4379,7 +4379,7 @@ dht_vgetxattr_alloc_and_fill (dht_local_t *local, dict_t *xattr, xlator_t *this,
local->alloc_len += strlen(value);
if (!local->xattr_val) {
- local->alloc_len += sizeof (DHT_PATHINFO_HEADER) + 10;
+ local->alloc_len += (SLEN (DHT_PATHINFO_HEADER) + 10);
local->xattr_val = GF_MALLOC (local->alloc_len,
gf_common_mt_char);
if (!local->xattr_val) {
@@ -5189,8 +5189,9 @@ dht_getxattr (call_frame_t *frame, xlator_t *this,
if (!DHT_IS_DIR(layout))
goto no_dht_is_dir;
- if (strncmp (key, GF_XATTR_GET_REAL_FILENAME_KEY,
- strlen (GF_XATTR_GET_REAL_FILENAME_KEY)) == 0) {
+ if ((strncmp (key, GF_XATTR_GET_REAL_FILENAME_KEY,
+ SLEN (GF_XATTR_GET_REAL_FILENAME_KEY)) == 0)
+ && DHT_IS_DIR(layout)) {
dht_getxattr_get_real_filename (frame, this, loc, key, xdata);
return 0;
}
@@ -5442,7 +5443,7 @@ dht_fgetxattr (call_frame_t *frame, xlator_t *this,
if ((fd->inode->ia_type == IA_IFDIR)
&& key
&& (strncmp (key, GF_XATTR_LOCKINFO_KEY,
- strlen (GF_XATTR_LOCKINFO_KEY)) != 0)) {
+ SLEN (GF_XATTR_LOCKINFO_KEY)) != 0)) {
local->call_cnt = conf->subvolume_cnt;
cnt = conf->subvolume_cnt;
ret = dht_inode_ctx_mdsvol_get (fd->inode, this, &mds_subvol);
@@ -6165,7 +6166,7 @@ dht_setxattr (call_frame_t *frame, xlator_t *this,
goto err;
}
- memcpy (value, tmp->data, ((tmp->len < 4095) ? tmp->len : 4095));
+ memcpy (value, tmp->data, min (tmp->len, 4095));
local->key = gf_strdup (value);
local->call_cnt = conf->subvolume_cnt;
@@ -6214,7 +6215,7 @@ dht_setxattr (call_frame_t *frame, xlator_t *this,
tmp = dict_get (xattr, "distribute.directory-spread-count");
if (tmp) {
/* Setxattr value is packed as 'binary', not string */
- memcpy (value, tmp->data, ((tmp->len < 4095)?tmp->len:4095));
+ memcpy (value, tmp->data, min (tmp->len, 4095));
ret = gf_string2uint32 (value, &dir_spread);
if (!ret && ((dir_spread <= conf->subvolume_cnt) &&
(dir_spread > 0))) {
@@ -11422,7 +11423,6 @@ dht_log_new_layout_for_dir_selfheal (xlator_t *this, loc_t *loc,
int i = 0;
gf_loglevel_t log_level = gf_log_get_loglevel();
int ret = 0;
- int max_string_len = 0;
if (log_level < GF_LOG_INFO)
return;
@@ -11439,9 +11439,7 @@ dht_log_new_layout_for_dir_selfheal (xlator_t *this, loc_t *loc,
if (!loc->path)
return;
- max_string_len = sizeof (string);
-
- ret = snprintf (string, max_string_len, "Setting layout of %s with ",
+ ret = snprintf (string, sizeof (string), "Setting layout of %s with ",
loc->path);
if (ret < 0)
@@ -11461,7 +11459,7 @@ dht_log_new_layout_for_dir_selfheal (xlator_t *this, loc_t *loc,
for (i = 0; i < layout->cnt; i++) {
- ret = snprintf (string, max_string_len,
+ ret = snprintf (string, sizeof (string),
"[Subvol_name: %s, Err: %d , Start: "
"%"PRIu32 " , Stop: %"PRIu32 " , Hash: %"
PRIu32 " ], ",
diff --git a/xlators/cluster/dht/src/dht-rebalance.c b/xlators/cluster/dht/src/dht-rebalance.c
index ed61880c8e4..9983429acec 100644
--- a/xlators/cluster/dht/src/dht-rebalance.c
+++ b/xlators/cluster/dht/src/dht-rebalance.c
@@ -147,12 +147,12 @@ dht_send_rebalance_event (xlator_t *this, int cmd, gf_defrag_status_t status)
volname = defrag->tier_conf.volname;
} else {
/* DHT volume */
- len = strlen (this->name);
+ len = strlen (this->name) - strlen (suffix);
tmpstr = gf_strdup (this->name);
if (tmpstr) {
- ptr = tmpstr + (len - strlen (suffix));
+ ptr = tmpstr + len;
if (!strcmp (ptr, suffix)) {
- tmpstr[len - strlen (suffix)] = '\0';
+ tmpstr[len] = '\0';
volname = tmpstr;
}
}
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,