From 65db3e596e456e11f524160261bf87a9ebe9fa00 Mon Sep 17 00:00:00 2001 From: N Balachandran Date: Fri, 7 Sep 2018 11:39:39 +0530 Subject: dht: Use snprintf instead of strncpy The recent changes to use malloc instead of calloc left the new_name and new_path non-null terminated. We now use snprintf instead of strncpy to fix this. Change-Id: I1a31701ca9447efde38921be0ba2c73cde2e7976 fixes: bz#1626346 Signed-off-by: N Balachandran --- xlators/cluster/dht/src/dht-helper.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/xlators/cluster/dht/src/dht-helper.c b/xlators/cluster/dht/src/dht-helper.c index 8bd3f564221..403f0a0f514 100644 --- a/xlators/cluster/dht/src/dht-helper.c +++ b/xlators/cluster/dht/src/dht-helper.c @@ -612,6 +612,14 @@ dht_frame_return (call_frame_t *frame) return this_call_cnt; } +/* + * Use this function to specify which subvol you want the file created + * on - this need not be the hashed subvol. + * Format: @name>: + * Eg: file-1@vol1-dht:vol1-client-0 + * where vol1 is a pure distribute volume + * will create file-1 on vol1-client-0 + */ int dht_filter_loc_subvol_key (xlator_t *this, loc_t *loc, loc_t *new_loc, @@ -634,24 +642,27 @@ dht_filter_loc_subvol_key (xlator_t *this, loc_t *loc, loc_t *new_loc, trav = this->children; while (trav) { - keylen = snprintf (key, sizeof (key), "*@%s:%s", this->name, trav->xlator->name); + keylen = snprintf (key, sizeof (key), "*@%s:%s", this->name, + trav->xlator->name); + /* Ignore '*' */ + keylen = keylen - 1; if (fnmatch (key, loc->name, FNM_NOESCAPE) == 0) { - name_len = strlen (loc->name); - new_name = GF_MALLOC(name_len, + name_len = strlen (loc->name) - keylen; + new_name = GF_MALLOC(name_len + 1, gf_common_mt_char); if (!new_name) goto out; if (fnmatch (key, loc->path, FNM_NOESCAPE) == 0) { - path_len = strlen (loc->path); - new_path = GF_MALLOC(path_len, + path_len = strlen (loc->path) - keylen; + new_path = GF_MALLOC(path_len + 1, gf_common_mt_char); if (!new_path) goto out; - strncpy (new_path, loc->path, (path_len - - keylen + 1)); + snprintf (new_path, path_len + 1, + "%s", loc->path); } - strncpy (new_name, loc->name, (name_len - - keylen + 1)); + snprintf (new_name, name_len + 1, "%s", + loc->name); if (new_loc) { new_loc->path = ((new_path) ? new_path: -- cgit