diff options
author | Yaniv Kaul <ykaul@redhat.com> | 2018-08-21 20:43:07 +0300 |
---|---|---|
committer | Amar Tumballi <amarts@redhat.com> | 2018-08-31 13:13:43 +0000 |
commit | 052849983e51a061d7fb2c3ffd74fa78bb257084 (patch) | |
tree | 5f82954bd3518c7eeab6ece16edbf72b0a3e0db1 /libglusterfs/src/stack.c | |
parent | 67cfacaa9d5ce31240a4d39e81b2984a57ac8865 (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/stack.c')
-rw-r--r-- | libglusterfs/src/stack.c | 10 |
1 files changed, 6 insertions, 4 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); |