From e1818e02408d9e412a2092c2419f323d6b8c7413 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Mon, 15 Oct 2018 10:00:20 -0700 Subject: tlsf_malloc: Set errno to ENOMEM on failure Set errno to ENOMEM on failure so that correct error message can be displayed by users who rely on errno. Signed-off-by: Andrey Smirnov Signed-off-by: Sascha Hauer --- common/tlsf_malloc.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'common/tlsf_malloc.c') diff --git a/common/tlsf_malloc.c b/common/tlsf_malloc.c index aa3ab23975..c8900fc6bb 100644 --- a/common/tlsf_malloc.c +++ b/common/tlsf_malloc.c @@ -28,6 +28,7 @@ extern tlsf_pool tlsf_mem_pool; void *malloc(size_t bytes) { + void *mem; /* * tlsf_malloc returns NULL for zero bytes, we instead want * to have a valid pointer. @@ -35,7 +36,11 @@ void *malloc(size_t bytes) if (!bytes) bytes = 1; - return tlsf_malloc(tlsf_mem_pool, bytes); + mem = tlsf_malloc(tlsf_mem_pool, bytes); + if (!mem) + errno = ENOMEM; + + return mem; } EXPORT_SYMBOL(malloc); @@ -47,13 +52,21 @@ EXPORT_SYMBOL(free); void *realloc(void *oldmem, size_t bytes) { - return tlsf_realloc(tlsf_mem_pool, oldmem, bytes); + void *mem = tlsf_realloc(tlsf_mem_pool, oldmem, bytes); + if (!mem) + errno = ENOMEM; + + return mem; } EXPORT_SYMBOL(realloc); void *memalign(size_t alignment, size_t bytes) { - return tlsf_memalign(tlsf_mem_pool, alignment, bytes); + void *mem = tlsf_memalign(tlsf_mem_pool, alignment, bytes); + if (!mem) + errno = ENOMEM; + + return mem; } EXPORT_SYMBOL(memalign); -- cgit v1.2.3