diff options
author | Dmitry Antipov <dmantipov@yandex.ru> | 2019-12-04 15:46:16 +0300 |
---|---|---|
committer | Xavi Hernandez <xhernandez@redhat.com> | 2019-12-05 10:44:48 +0000 |
commit | 3199f8759ebfcc31ce158201c3c25d9a738479f8 (patch) | |
tree | 1f7f6b882b6b84c0ee6c32c2ef181b1fa3019240 /libglusterfs | |
parent | dc757b2c97f5917885db76c62fa4fb429ae7b9ab (diff) |
core: avoid NULL pointer dereference
Since low-level __gf_xxx allocation function may be called before
ctx is initialized, add extra check for NULL where appropriate.
Fixes: bz#1776784
Change-Id: I3127fa4b93f8e3e5846106aadcfed1baa27ac43f
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
Diffstat (limited to 'libglusterfs')
-rw-r--r-- | libglusterfs/src/mem-pool.c | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/libglusterfs/src/mem-pool.c b/libglusterfs/src/mem-pool.c index d0f8a64d2f7..ea9cc6881b1 100644 --- a/libglusterfs/src/mem-pool.c +++ b/libglusterfs/src/mem-pool.c @@ -123,6 +123,15 @@ gf_mem_update_acct_info(struct mem_acct *mem_acct, struct mem_header *header, return gf_mem_header_prepare(header, size); } +static bool +gf_mem_acct_enabled(void) +{ + xlator_t *x = THIS; + /* Low-level __gf_xxx() may be called + before ctx is initialized. */ + return x->ctx && x->ctx->mem_acct_enable; +} + void * __gf_calloc(size_t nmemb, size_t size, uint32_t type, const char *typestr) { @@ -131,7 +140,7 @@ __gf_calloc(size_t nmemb, size_t size, uint32_t type, const char *typestr) void *ptr = NULL; xlator_t *xl = NULL; - if (!THIS->ctx->mem_acct_enable) + if (!gf_mem_acct_enabled()) return CALLOC(nmemb, size); xl = THIS; @@ -156,7 +165,7 @@ __gf_malloc(size_t size, uint32_t type, const char *typestr) void *ptr = NULL; xlator_t *xl = NULL; - if (!THIS->ctx->mem_acct_enable) + if (!gf_mem_acct_enabled()) return MALLOC(size); xl = THIS; @@ -178,7 +187,7 @@ __gf_realloc(void *ptr, size_t size) size_t tot_size = 0; struct mem_header *header = NULL; - if (!THIS->ctx->mem_acct_enable) + if (!gf_mem_acct_enabled()) return REALLOC(ptr, size); REQUIRE(NULL != ptr); @@ -301,7 +310,7 @@ __gf_free(void *free_ptr) struct mem_header *header = NULL; bool last_ref = false; - if (!THIS->ctx->mem_acct_enable) { + if (!gf_mem_acct_enabled()) { FREE(free_ptr); return; } |