diff options
author | Yaniv Kaul <ykaul@redhat.com> | 2018-08-08 22:28:39 +0300 |
---|---|---|
committer | Amar Tumballi <amarts@redhat.com> | 2018-08-20 03:12:21 +0000 |
commit | 9d426f3522422e7499f747801b1894164a4b1589 (patch) | |
tree | 8f9192d8b90d535b502786c88292d6276a9318d7 /libglusterfs | |
parent | d7f97cef9099e8904d296df5cc9a221d295cfb35 (diff) |
libglusterfs/src/common-utils.c: strncpy -> sprintf, remove dead code.
strncpy may not be very efficient for sort 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, do a quick calc to see how much we really need and use
snprintf() to copy as much.
Also, move from CALLOC to MALLOC, as we are writing to this newly
allocated memory right away and add terminating null.
Lastly, removed some dead code. I did the same optimization as above
to it, only to find out no one is using it.
Compile-tested only!
Change-Id: Ib91b9a73c3d74c511fd067446b1bf6c2e1802687
updates: bz#1193929
Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
Diffstat (limited to 'libglusterfs')
-rw-r--r-- | libglusterfs/src/common-utils.c | 125 | ||||
-rw-r--r-- | libglusterfs/src/common-utils.h | 4 |
2 files changed, 5 insertions, 124 deletions
diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c index 374b76b0410..9dd030aa6c4 100644 --- a/libglusterfs/src/common-utils.c +++ b/libglusterfs/src/common-utils.c @@ -105,8 +105,9 @@ mkdir_p (char *path, mode_t mode, gf_boolean_t allow_symlinks) char dir[PATH_MAX] = {0,}; struct stat stbuf = {0,}; - strncpy (dir, path, (PATH_MAX - 1)); - dir[PATH_MAX - 1] = '\0'; + const int path_len = min(strlen(path), PATH_MAX -1); + + snprintf(dir, path_len + 1, "%s", path); i = (dir[0] == '/')? 1: 0; do { @@ -792,73 +793,6 @@ gf_trim (char *string) } int -gf_strsplit (const char *str, const char *delim, - char ***tokens, int *token_count) -{ - char *_running = NULL; - char *running = NULL; - char *token = NULL; - char **token_list = NULL; - int count = 0; - int i = 0; - int j = 0; - - if (str == NULL || delim == NULL || tokens == NULL || token_count == NULL) { - gf_msg_callingfn (THIS->name, GF_LOG_WARNING, EINVAL, - LG_MSG_INVALID_ARG, "argument invalid"); - return -1; - } - - _running = gf_strdup (str); - if (_running == NULL) - return -1; - - running = _running; - - while ((token = strsep (&running, delim)) != NULL) { - if (token[0] != '\0') - count++; - } - GF_FREE (_running); - - _running = gf_strdup (str); - if (_running == NULL) - return -1; - - running = _running; - - if ((token_list = GF_CALLOC (count, sizeof (char *), - gf_common_mt_char)) == NULL) { - GF_FREE (_running); - return -1; - } - - while ((token = strsep (&running, delim)) != NULL) { - if (token[0] == '\0') - continue; - - token_list[i] = gf_strdup (token); - if (token_list[i] == NULL) - goto free_exit; - i++; - } - - GF_FREE (_running); - - *tokens = token_list; - *token_count = count; - return 0; - -free_exit: - GF_FREE (_running); - for (j = 0; j < i; j++) - GF_FREE (token_list[j]); - - GF_FREE (token_list); - return -1; -} - -int gf_strstr (const char *str, const char *delim, const char *match) { char *tmp = NULL; @@ -2108,54 +2042,6 @@ nwstrtail (char *str, char *pattern) return *pattern ? NULL : str; } -void -skipword (char **s) -{ - if (!*s) - return; - - skipwhite (s); - - while (!isspace(**s)) - (*s)++; -} - -char * -get_nth_word (const char *str, int n) -{ - char buf[4096] = {0}; - char *start = NULL; - char *word = NULL; - int i = 0; - int word_len = 0; - const char *end = NULL; - - if (!str) - goto out; - - snprintf (buf, sizeof (buf), "%s", str); - start = buf; - - for (i = 0; i < n-1; i++) - skipword (&start); - - skipwhite (&start); - end = strpbrk ((const char *)start, " \t\n\0"); - - if (!end) - goto out; - - word_len = labs (end - start); - - word = GF_CALLOC (1, word_len + 1, gf_common_mt_strdup); - if (!word) - goto out; - - strncpy (word, start, word_len); - *(word + word_len) = '\0'; - out: - return word; -} /** * token_iter_init -- initialize tokenization @@ -3013,7 +2899,7 @@ gf_strip_whitespace (char *str, int len) GF_ASSERT (str); - new_str = GF_CALLOC (1, len + 1, gf_common_mt_char); + new_str = GF_MALLOC (len + 1, gf_common_mt_char); if (new_str == NULL) return -1; @@ -3024,8 +2910,7 @@ gf_strip_whitespace (char *str, int len) new_str[new_len] = '\0'; if (new_len != len) { - memset (str, 0, len); - strncpy (str, new_str, new_len); + snprintf(str, new_len + 1, "%s", new_str); } GF_FREE (new_str); diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h index 55ef916ddf7..4fd92b0315b 100644 --- a/libglusterfs/src/common-utils.h +++ b/libglusterfs/src/common-utils.h @@ -750,8 +750,6 @@ int32_t gf_roundup_power_of_two (int32_t nr); int32_t gf_roundup_next_power_of_two (int32_t nr); char *gf_trim (char *string); -int gf_strsplit (const char *str, const char *delim, - char ***tokens, int *token_count); int gf_volume_name_validate (const char *volume_name); int gf_string2long (const char *str, long *n); @@ -803,9 +801,7 @@ char *gf_resolve_path_parent (const char *path); char *strtail (char *str, const char *pattern); void skipwhite (char **s); char *nwstrtail (char *str, char *pattern); -void skip_word (char **str); /* returns a new string with nth word of given string. n>=1 */ -char *get_nth_word (const char *str, int n); typedef struct token_iter { char *end; |