summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src
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 /libglusterfs/src
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>
Diffstat (limited to 'libglusterfs/src')
-rw-r--r--libglusterfs/src/stack.c10
-rw-r--r--libglusterfs/src/statedump.c70
-rw-r--r--libglusterfs/src/store.c6
3 files changed, 47 insertions, 39 deletions
diff --git a/libglusterfs/src/stack.c b/libglusterfs/src/stack.c
index 2987441bb6d..037f565aba3 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 741b2b97555..057499eef68 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 15a888d36c0..5172982149f 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;