1
0
mirror of https://github.com/gluster/glusterfs.git synced 2026-02-05 06:47:35 +01:00

libglusterfs: fix dict leak

Problem:
gf_rev_dns_lookup_cached() allocated struct dnscache->dict if it was null
but the freeing was left to the caller.

Fix:
Moved dict allocation and freeing into corresponding init and fini
routines so that its easier for the caller to avoid such leaks.

Updates: #1000
Change-Id: I90d6a6f85ca2dd4fe0ab461177aaa9ac9c1fbcf9
Signed-off-by: Ravishankar N <ravishankar@redhat.com>
This commit is contained in:
Ravishankar N
2020-09-01 11:36:42 +05:30
committed by Xavi Hernandez
parent cede859c73
commit 079f7a7d8a
4 changed files with 30 additions and 10 deletions

View File

@@ -577,14 +577,34 @@ struct dnscache *
gf_dnscache_init(time_t ttl)
{
struct dnscache *cache = GF_MALLOC(sizeof(*cache), gf_common_mt_dnscache);
if (cache) {
cache->cache_dict = NULL;
if (!cache)
return NULL;
cache->cache_dict = dict_new();
if (!cache->cache_dict) {
GF_FREE(cache);
cache = NULL;
} else {
cache->ttl = ttl;
}
return cache;
}
/**
* gf_dnscache_deinit -- cleanup resources used by struct dnscache
*/
void
gf_dnscache_deinit(struct dnscache *cache)
{
if (!cache) {
gf_msg_plain(GF_LOG_WARNING, "dnscache is NULL");
return;
}
dict_unref(cache->cache_dict);
GF_FREE(cache);
}
/**
* gf_dnscache_entry_init -- Initialize a dnscache entry
*
@@ -633,12 +653,6 @@ gf_rev_dns_lookup_cached(const char *ip, struct dnscache *dnscache)
if (!dnscache)
goto out;
if (!dnscache->cache_dict) {
dnscache->cache_dict = dict_new();
if (!dnscache->cache_dict) {
goto out;
}
}
cache = dnscache->cache_dict;
/* Quick cache lookup to see if we already hold it */

View File

@@ -255,6 +255,8 @@ list_node_del(struct list_node *node);
struct dnscache *
gf_dnscache_init(time_t ttl);
void
gf_dnscache_deinit(struct dnscache *cache);
struct dnscache_entry *
gf_dnscache_entry_init(void);
void

View File

@@ -585,6 +585,7 @@ gf_dirent_free
gf_dirent_orig_offset
gf_dm_hashfn
gf_dnscache_init
gf_dnscache_deinit
gf_errno_to_error
gf_error_to_errno
_gf_event

View File

@@ -3838,7 +3838,7 @@ ios_conf_destroy(struct ios_conf *conf)
_ios_destroy_dump_thread(conf);
ios_destroy_sample_buf(conf->ios_sample_buf);
LOCK_DESTROY(&conf->lock);
GF_FREE(conf->dnscache);
gf_dnscache_deinit(conf->dnscache);
GF_FREE(conf);
}
@@ -3950,11 +3950,14 @@ init(xlator_t *this)
gf_log(this->name, GF_LOG_ERROR, "Out of memory.");
goto out;
}
ret = -1;
GF_OPTION_INIT("ios-dnscache-ttl-sec", conf->ios_dnscache_ttl_sec, int32,
out);
conf->dnscache = gf_dnscache_init(conf->ios_dnscache_ttl_sec);
if (!conf->dnscache) {
ret = -1;
goto out;
}
GF_OPTION_INIT("sys-log-level", sys_log_str, str, out);
if (sys_log_str) {