diff options
author | Yaniv Kaul <ykaul@redhat.com> | 2018-08-21 20:42:53 +0300 |
---|---|---|
committer | Amar Tumballi <amarts@redhat.com> | 2018-08-31 06:15:46 +0000 |
commit | 7772315bd7d82d5f06f008dfe767f1e597a41b23 (patch) | |
tree | aa766b174482be034228698a994c8bbf9ac32b84 /xlators/storage/posix/src/posix-handle.c | |
parent | dc6e6b71f87f6f89bb0b69816e92779595d716bd (diff) |
multiple xlators (storage/posix): strncpy()->sprintf(), reduce strlen()'s
xlators/storage/posix/src/posix-gfid-path.c
xlators/storage/posix/src/posix-handle.c
xlators/storage/posix/src/posix-helpers.c
xlators/storage/posix/src/posix-inode-fd-ops.c
xlators/storage/posix/src/posix.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().
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: I056939f111a4ec6bc8ebd539ebcaf9eb67da6c95
updates: bz#1193929
Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
Diffstat (limited to 'xlators/storage/posix/src/posix-handle.c')
-rw-r--r-- | xlators/storage/posix/src/posix-handle.c | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/xlators/storage/posix/src/posix-handle.c b/xlators/storage/posix/src/posix-handle.c index 2c88bc63809..802dcf1cc2a 100644 --- a/xlators/storage/posix/src/posix-handle.c +++ b/xlators/storage/posix/src/posix-handle.c @@ -114,9 +114,7 @@ posix_make_ancestral_node (const char *priv_base_path, char *path, int pathsize, entry->inode = inode_ref (inode); list_add_tail (&entry->list, &head->list); - strcpy (real_path, priv_base_path); - strcat (real_path, "/"); - strcat (real_path, path); + snprintf(real_path, sizeof (real_path), "%s/%s", priv_base_path, path); loc.inode = inode_ref (inode); gf_uuid_copy (loc.gfid, inode->gfid); @@ -173,7 +171,7 @@ posix_make_ancestryfromgfid (xlator_t *this, char *path, int pathsize, *parent = inode_ref (itable->root); - saved_dir = alloca(strlen("/") + 1); + saved_dir = alloca(sizeof ("/")); strcpy(saved_dir, "/"); dir_stack[top] = saved_dir; break; @@ -498,7 +496,7 @@ posix_handle_gfid_path (xlator_t *this, uuid_t gfid, const char *basename, len = snprintf (buf, buflen, "%s/%s", priv->base_path, basename); } else { - strncpy (buf, priv->base_path, buflen); + len = snprintf (buf, buflen, "%s", priv->base_path); } goto out; } @@ -536,8 +534,8 @@ posix_handle_init (xlator_t *this) return -1; } - handle_pfx = alloca (priv->base_path_length + 1 + strlen (GF_HIDDEN_PATH) - + 1); + handle_pfx = alloca (priv->base_path_length + 1 + + SLEN (GF_HIDDEN_PATH) + 1); sprintf (handle_pfx, "%s/%s", priv->base_path, GF_HIDDEN_PATH); @@ -716,16 +714,18 @@ posix_handle_trash_init (xlator_t *this) priv = this->private; - priv->trash_path = GF_CALLOC (1, priv->base_path_length + strlen ("/") - + strlen (GF_HIDDEN_PATH) + strlen ("/") - + strlen (TRASH_DIR) + 1, + priv->trash_path = GF_MALLOC (priv->base_path_length + SLEN ("/") + + SLEN (GF_HIDDEN_PATH) + SLEN ("/") + + SLEN (TRASH_DIR) + 1, gf_posix_mt_trash_path); if (!priv->trash_path) goto out; - strncpy (priv->trash_path, priv->base_path, priv->base_path_length); - strcat (priv->trash_path, "/" GF_HIDDEN_PATH "/" TRASH_DIR); + snprintf (priv->trash_path, priv->base_path_length + + SLEN (GF_HIDDEN_PATH) + SLEN (TRASH_DIR) + 3, + "%s/%s/%s", priv->base_path, GF_HIDDEN_PATH, TRASH_DIR); + ret = posix_handle_new_trash_init (this, priv->trash_path); if (ret) goto out; |