mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: barebox@lists.infradead.org
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
Subject: [PATCH 4/5] tlsf_malloc: Set errno to ENOMEM on failure
Date: Mon, 15 Oct 2018 10:00:20 -0700	[thread overview]
Message-ID: <20181015170021.22617-4-andrew.smirnov@gmail.com> (raw)
In-Reply-To: <20181015170021.22617-1-andrew.smirnov@gmail.com>

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 <andrew.smirnov@gmail.com>
---
 common/tlsf_malloc.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/common/tlsf_malloc.c b/common/tlsf_malloc.c
index aa3ab2397..c8900fc6b 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);
 
-- 
2.17.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2018-10-15 17:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-15 17:00 [PATCH 1/5] tlsf_malloc: dummy_malloc: Share code for calloc() Andrey Smirnov
2018-10-15 17:00 ` [PATCH 2/5] dummy_malloc: Make use of PTR_ALIGN Andrey Smirnov
2018-10-15 17:00 ` [PATCH 3/5] dummy_malloc: Check if sbrk() fails Andrey Smirnov
2018-10-15 17:00 ` Andrey Smirnov [this message]
2018-10-15 17:00 ` [PATCH 5/5] libfile: Error out if out of memory in read_file_2() Andrey Smirnov
2018-10-16  7:04 ` [PATCH 1/5] tlsf_malloc: dummy_malloc: Share code for calloc() Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181015170021.22617-4-andrew.smirnov@gmail.com \
    --to=andrew.smirnov@gmail.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox