From 052849983e51a061d7fb2c3ffd74fa78bb257084 Mon Sep 17 00:00:00 2001 From: Yaniv Kaul Date: Tue, 21 Aug 2018 20:43:07 +0300 Subject: 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 --- libglusterfs/src/stack.c | 10 ++-- libglusterfs/src/statedump.c | 70 ++++++++++++----------- libglusterfs/src/store.c | 6 +- rpc/rpc-lib/src/rpc-clnt.c | 10 ++-- xlators/features/locks/src/posix.c | 7 ++- xlators/features/marker/src/marker-quota-helper.c | 6 +- xlators/features/marker/src/marker-quota.c | 6 +- 7 files changed, 68 insertions(+), 47 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; diff --git a/rpc/rpc-lib/src/rpc-clnt.c b/rpc/rpc-lib/src/rpc-clnt.c index 5e1d41311d0..a4663648a38 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 c4ca71cfd7f..22d5990275d 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 e3e218f8af5..92d4094e7f8 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 f6a0df810bc..620201d616e 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) -- cgit