[PATCH 1/2] sandbox: libc_malloc: populate barebox errno on error

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Nov 22 09:00:06 PST 2023


TLSF already populates errno on errors, so do likewise for the
allocators that don't.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 arch/sandbox/os/libc_malloc.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/arch/sandbox/os/libc_malloc.c b/arch/sandbox/os/libc_malloc.c
index 74e3e2680585..975c41b0ec49 100644
--- a/arch/sandbox/os/libc_malloc.c
+++ b/arch/sandbox/os/libc_malloc.c
@@ -6,18 +6,30 @@
 #include <stdlib.h>
 #include <malloc.h>
 
+#define BAREBOX_ENOMEM 12
+extern int barebox_errno;
+
 void barebox_malloc_stats(void)
 {
 }
 
 void *barebox_memalign(size_t alignment, size_t bytes)
 {
-	return memalign(alignment, bytes);
+	void *mem = memalign(alignment, bytes);
+	if (!mem)
+		barebox_errno = BAREBOX_ENOMEM;
+
+	return mem;
 }
 
 void *barebox_malloc(size_t size)
 {
-	return malloc(size);
+
+	void *mem = malloc(size);
+	if (!mem)
+		barebox_errno = BAREBOX_ENOMEM;
+
+	return mem;
 }
 
 void barebox_free(void *ptr)
@@ -27,10 +39,18 @@ void barebox_free(void *ptr)
 
 void *barebox_realloc(void *ptr, size_t size)
 {
-	return realloc(ptr, size);
+	void *mem = realloc(ptr, size);
+	if (!mem)
+		barebox_errno = BAREBOX_ENOMEM;
+
+	return mem;
 }
 
 void *barebox_calloc(size_t n, size_t elem_size)
 {
-	return calloc(n, elem_size);
+	void *mem = calloc(n, elem_size);
+	if (!mem)
+		barebox_errno = BAREBOX_ENOMEM;
+
+	return mem;
 }
-- 
2.39.2




More information about the barebox mailing list