diff options
author | Yaniv Kaul <ykaul@redhat.com> | 2018-08-21 19:20:25 +0300 |
---|---|---|
committer | Amar Tumballi <amarts@redhat.com> | 2018-08-31 01:36:34 +0000 |
commit | 4b5e3c347cfb80e4749f1963e25d1dd93563f784 (patch) | |
tree | f82b1948aec0110f46d3c1e2b230c6971871f413 /xlators/features/bit-rot/src/bitd/bit-rot-scrub-status.c | |
parent | 4996a229f3d09cbb6ed3b9dcdf1d527f36803919 (diff) |
bit-rot xlator: strncpy()->sprintf(), reduce strlen()'s
xlators/features/bit-rot/src/bitd/bit-rot-scrub-status.c
xlators/features/bit-rot/src/stub/bit-rot-stub-helpers.c
xlators/features/bit-rot/src/stub/bit-rot-stub.c
xlators/features/bit-rot/src/stub/bit-rot-stub.h
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(). Ensure sprintf() results do not
truncate.
Also:
- save the result of strlen() and re-use it when possible.
- move from strlen to SLEN or sizeof() for const strings.
- move ret from int32 to int.
Compile-tested only!
Change-Id: Ib9b923c45d2d59ac15a105410e8160b252061018
updates: bz#1193929
Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
Diffstat (limited to 'xlators/features/bit-rot/src/bitd/bit-rot-scrub-status.c')
-rw-r--r-- | xlators/features/bit-rot/src/bitd/bit-rot-scrub-status.c | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/xlators/features/bit-rot/src/bitd/bit-rot-scrub-status.c b/xlators/features/bit-rot/src/bitd/bit-rot-scrub-status.c index 2f9aaf31a52..210beca7e2f 100644 --- a/xlators/features/bit-rot/src/bitd/bit-rot-scrub-status.c +++ b/xlators/features/bit-rot/src/bitd/bit-rot-scrub-status.c @@ -9,6 +9,7 @@ */ #include <string.h> +#include <stdio.h> #include "bit-rot-scrub-status.h" @@ -61,6 +62,9 @@ br_update_scrub_finish_time (br_scrub_stats_t *scrub_stat, char *timestr, return; lst_size = sizeof (scrub_stat->last_scrub_time); + if (strlen (timestr) >= lst_size) + return; + pthread_mutex_lock (&scrub_stat->lock); { scrub_stat->scrub_end_tv.tv_sec = tv->tv_sec; @@ -69,9 +73,8 @@ br_update_scrub_finish_time (br_scrub_stats_t *scrub_stat, char *timestr, scrub_stat->scrub_end_tv.tv_sec - scrub_stat->scrub_start_tv.tv_sec; - strncpy (scrub_stat->last_scrub_time, timestr, - lst_size-1); - scrub_stat->last_scrub_time[lst_size-1] = '\0'; + snprintf (scrub_stat->last_scrub_time, lst_size, "%s", + timestr); } pthread_mutex_unlock (&scrub_stat->lock); } |