summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYaniv Kaul <ykaul@redhat.com>2018-08-21 20:43:07 +0300
committerAmar Tumballi <amarts@redhat.com>2018-08-31 13:13:43 +0000
commit052849983e51a061d7fb2c3ffd74fa78bb257084 (patch)
tree5f82954bd3518c7eeab6ece16edbf72b0a3e0db1
parent67cfacaa9d5ce31240a4d39e81b2984a57ac8865 (diff)
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 <ykaul@redhat.com>
-rw-r--r--libglusterfs/src/stack.c10
-rw-r--r--libglusterfs/src/statedump.c70
-rw-r--r--libglusterfs/src/store.c6
-rw-r--r--rpc/rpc-lib/src/rpc-clnt.c10
-rw-r--r--xlators/features/locks/src/posix.c7
-rw-r--r--xlators/features/marker/src/marker-quota-helper.c6
-rw-r--r--xlators/features/marker/src/marker-quota.c6
7 files changed, 68 insertions, 47 deletions
diff --git a/libglusterfs/src/stack.c b/libglusterfs/src/stack.c
index 2987441..037f565 100644
--- a/libglusterfs/src/stack.c
+++ b/libglusterfs/src/stack.c
@@ -91,6 +91,7 @@ gf_proc_dump_call_frame (call_frame_t *call_frame, const char *key_buf,...)
call_frame_t my_frame;
int ret = -1;
char timestr[256] = {0,};
+ int len;
if (!call_frame)
return;
@@ -111,8 +112,8 @@ gf_proc_dump_call_frame (call_frame_t *call_frame, const char *key_buf,...)
if (my_frame.root->ctx->measure_latency) {
gf_time_fmt (timestr, sizeof (timestr), my_frame.begin.tv_sec,
gf_timefmt_FT);
- snprintf (timestr + strlen (timestr),
- sizeof (timestr) - strlen (timestr),
+ len = strlen (timestr);
+ snprintf (timestr + len, sizeof (timestr) - len,
".%"GF_PRI_SNSECONDS, my_frame.begin.tv_nsec);
gf_proc_dump_write("frame-creation-time", "%s", timestr);
gf_proc_dump_write("timings", "%ld.%"GF_PRI_SNSECONDS
@@ -161,6 +162,7 @@ gf_proc_dump_call_stack (call_stack_t *call_stack, const char *key_buf,...)
call_frame_t *trav;
int32_t i = 1, cnt = 0;
char timestr[256] = {0,};
+ int len;
if (!call_stack)
return;
@@ -174,8 +176,8 @@ gf_proc_dump_call_stack (call_stack_t *call_stack, const char *key_buf,...)
cnt = call_frames_count (call_stack);
gf_time_fmt (timestr, sizeof (timestr), call_stack->tv.tv_sec,
gf_timefmt_FT);
- snprintf (timestr + strlen (timestr),
- sizeof (timestr) - strlen (timestr),
+ len = strlen (timestr);
+ snprintf (timestr + len, sizeof (timestr) - len,
".%"GF_PRI_SNSECONDS, call_stack->tv.tv_nsec);
gf_proc_dump_write("callstack-creation-time", "%s", timestr);
diff --git a/libglusterfs/src/statedump.c b/libglusterfs/src/statedump.c
index 741b2b9..057499e 100644
--- a/libglusterfs/src/statedump.c
+++ b/libglusterfs/src/statedump.c
@@ -118,24 +118,25 @@ out:
return ret;
}
-int
+static int
gf_proc_dump_add_section_fd (char *key, va_list ap)
{
char buf[GF_DUMP_MAX_BUF_LEN];
+ int len;
GF_ASSERT(key);
- snprintf (buf, GF_DUMP_MAX_BUF_LEN, "\n[");
- vsnprintf (buf + strlen(buf),
- GF_DUMP_MAX_BUF_LEN - strlen (buf), key, ap);
- snprintf (buf + strlen(buf),
- GF_DUMP_MAX_BUF_LEN - strlen (buf), "]\n");
- return sys_write (gf_dump_fd, buf, strlen (buf));
+ len = snprintf (buf, GF_DUMP_MAX_BUF_LEN, "\n[");
+ len += vsnprintf (buf + len,
+ GF_DUMP_MAX_BUF_LEN - len, key, ap);
+ len += snprintf (buf + len,
+ GF_DUMP_MAX_BUF_LEN - len, "]\n");
+ return sys_write (gf_dump_fd, buf, len);
}
-int
+static int
gf_proc_dump_add_section_strfd (char *key, va_list ap)
{
int ret = 0;
@@ -165,27 +166,24 @@ gf_proc_dump_add_section (char *key, ...)
}
-int
+static int
gf_proc_dump_write_fd (char *key, char *value, va_list ap)
{
char buf[GF_DUMP_MAX_BUF_LEN];
- int offset = 0;
+ int len = 0;
GF_ASSERT (key);
- offset = snprintf (buf, GF_DUMP_MAX_BUF_LEN, "%s", key);
- snprintf (buf + offset, GF_DUMP_MAX_BUF_LEN - offset, "=");
- offset += 1;
- vsnprintf (buf + offset, GF_DUMP_MAX_BUF_LEN - offset, value, ap);
+ len = snprintf (buf, GF_DUMP_MAX_BUF_LEN, "%s=", key);
+ len += vsnprintf (buf + len, GF_DUMP_MAX_BUF_LEN - len, value, ap);
- offset = strlen (buf);
- snprintf (buf + offset, GF_DUMP_MAX_BUF_LEN - offset, "\n");
- return sys_write (gf_dump_fd, buf, strlen (buf));
+ len += snprintf (buf + len, GF_DUMP_MAX_BUF_LEN - len, "\n");
+ return sys_write (gf_dump_fd, buf, len);
}
-int
+static int
gf_proc_dump_write_strfd (char *key, char *value, va_list ap)
{
int ret = 0;
@@ -506,7 +504,7 @@ gf_proc_dump_single_xlator_info (xlator_t *trav)
if (GF_PROC_DUMP_IS_XL_OPTION_ENABLED (inode) &&
(trav->itable)) {
- snprintf (itable_key, 1024, "%d.%s.itable",
+ snprintf (itable_key, sizeof (itable_key), "%d.%s.itable",
ctx->graph_id, trav->name);
}
@@ -689,6 +687,7 @@ gf_proc_dump_parse_set_option (char *key, char *value)
gf_boolean_t opt_value = _gf_false;
char buf[GF_DUMP_MAX_BUF_LEN];
int ret = -1;
+ int len;
if (!strcasecmp (key, "all")) {
(void)gf_proc_dump_enable_all_options ();
@@ -715,14 +714,16 @@ gf_proc_dump_parse_set_option (char *key, char *value)
if (!opt_key) {
//None of dump options match the key, return back
- snprintf (buf, sizeof (buf), "[Warning]:None of the options "
+ len = snprintf (buf, sizeof (buf), "[Warning]:None of the options "
"matched key : %s\n", key);
- ret = sys_write (gf_dump_fd, buf, strlen (buf));
-
- if (ret >= 0)
+ if (len < 0)
ret = -1;
+ else {
+ ret = sys_write (gf_dump_fd, buf, len);
+ if (ret >= 0)
+ ret = -1;
+ }
goto out;
-
}
opt_value = (strncasecmp (value, "yes", 3) ?
@@ -820,6 +821,7 @@ gf_proc_dump_info (int signum, glusterfs_ctx_t *ctx)
xlator_t *top = NULL;
xlator_list_t **trav_p = NULL;
int brick_count = 0;
+ int len = 0;
gf_proc_dump_lock ();
@@ -840,7 +842,7 @@ gf_proc_dump_info (int signum, glusterfs_ctx_t *ctx)
if (ctx->cmd_args.brick_name) {
GF_REMOVE_SLASH_FROM_PATH (ctx->cmd_args.brick_name, brick_name);
} else
- strncpy (brick_name, "glusterdump", sizeof (brick_name));
+ snprintf(brick_name, sizeof (brick_name), "glusterdump");
ret = gf_proc_dump_options_init ();
if (ret < 0)
@@ -870,16 +872,17 @@ gf_proc_dump_info (int signum, glusterfs_ctx_t *ctx)
ret = gettimeofday (&tv, NULL);
if (0 == ret) {
gf_time_fmt (timestr, sizeof timestr, tv.tv_sec, gf_timefmt_FT);
- snprintf (timestr + strlen (timestr),
- sizeof timestr - strlen (timestr),
+ len = strlen (timestr);
+ snprintf (timestr + len,
+ sizeof timestr - len,
".%"GF_PRI_SUSECONDS, tv.tv_usec);
}
- snprintf (sign_string, sizeof (sign_string), "DUMP-START-TIME: %s\n",
+ len = snprintf (sign_string, sizeof (sign_string), "DUMP-START-TIME: %s\n",
timestr);
//swallow the errors of write for start and end marker
- ret = sys_write (gf_dump_fd, sign_string, strlen (sign_string));
+ ret = sys_write (gf_dump_fd, sign_string, len);
memset (timestr, 0, sizeof (timestr));
@@ -921,14 +924,15 @@ gf_proc_dump_info (int signum, glusterfs_ctx_t *ctx)
ret = gettimeofday (&tv, NULL);
if (0 == ret) {
gf_time_fmt (timestr, sizeof timestr, tv.tv_sec, gf_timefmt_FT);
- snprintf (timestr + strlen (timestr),
- sizeof timestr - strlen (timestr),
+ len = strlen (timestr);
+ snprintf (timestr + len,
+ sizeof timestr - len,
".%"GF_PRI_SUSECONDS, tv.tv_usec);
}
- snprintf (sign_string, sizeof (sign_string), "\nDUMP-END-TIME: %s",
+ len = snprintf (sign_string, sizeof (sign_string), "\nDUMP-END-TIME: %s",
timestr);
- ret = sys_write (gf_dump_fd, sign_string, strlen (sign_string));
+ ret = sys_write (gf_dump_fd, sign_string, len);
if (gf_dump_fd != -1)
gf_proc_dump_close ();
diff --git a/libglusterfs/src/store.c b/libglusterfs/src/store.c
index 15a888d..5172982 100644
--- a/libglusterfs/src/store.c
+++ b/libglusterfs/src/store.c
@@ -491,8 +491,10 @@ gf_store_iter_new (gf_store_handle_t *shandle, gf_store_iter_t **iter)
if (!tmp_iter)
goto out;
- strncpy (tmp_iter->filepath, shandle->path, sizeof (tmp_iter->filepath));
- tmp_iter->filepath[sizeof (tmp_iter->filepath) - 1] = 0;
+ if (snprintf (tmp_iter->filepath, sizeof (tmp_iter->filepath), "%s",
+ shandle->path) >= sizeof (tmp_iter->filepath))
+ goto out;
+
tmp_iter->file = fp;
*iter = tmp_iter;
diff --git a/rpc/rpc-lib/src/rpc-clnt.c b/rpc/rpc-lib/src/rpc-clnt.c
index 5e1d413..a466364 100644
--- a/rpc/rpc-lib/src/rpc-clnt.c
+++ b/rpc/rpc-lib/src/rpc-clnt.c
@@ -106,6 +106,7 @@ call_bail (void *data)
struct timespec timeout = {0,};
char peerid[UNIX_PATH_MAX] = {0};
gf_boolean_t need_unref = _gf_false;
+ int len;
GF_VALIDATE_OR_GOTO ("client", data, out);
@@ -169,8 +170,8 @@ call_bail (void *data)
list_for_each_entry_safe (trav, tmp, &list, list) {
gf_time_fmt (frame_sent, sizeof frame_sent,
trav->saved_at.tv_sec, gf_timefmt_FT);
- snprintf (frame_sent + strlen (frame_sent),
- 256 - strlen (frame_sent),
+ len = strlen (frame_sent);
+ snprintf (frame_sent + len, sizeof (frame_sent) - len,
".%"GF_PRI_SUSECONDS, trav->saved_at.tv_usec);
gf_log (conn->name, GF_LOG_ERROR,
@@ -324,14 +325,15 @@ saved_frames_unwind (struct saved_frames *saved_frames)
struct saved_frame *trav = NULL;
struct saved_frame *tmp = NULL;
char timestr[1024] = {0,};
+ int len;
list_splice_init (&saved_frames->lk_sf.list, &saved_frames->sf.list);
list_for_each_entry_safe (trav, tmp, &saved_frames->sf.list, list) {
gf_time_fmt (timestr, sizeof timestr,
trav->saved_at.tv_sec, gf_timefmt_FT);
- snprintf (timestr + strlen (timestr),
- sizeof(timestr) - strlen (timestr),
+ len = strlen (timestr);
+ snprintf (timestr + len, sizeof(timestr) - len,
".%"GF_PRI_SUSECONDS, trav->saved_at.tv_usec);
if (!trav->rpcreq || !trav->rpcreq->prog)
diff --git a/xlators/features/locks/src/posix.c b/xlators/features/locks/src/posix.c
index c4ca71c..22d5990 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;
diff --git a/xlators/features/marker/src/marker-quota-helper.c b/xlators/features/marker/src/marker-quota-helper.c
index e3e218f..92d4094 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 f6a0df8..620201d 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)