The malloc/realloc/memalign functions in newlib do not set errno when
they fail. Here is a patch to correct the problem.
2005-10-06 Bob Wilson <bob.wilson@acm.org>
* libc/stdlib/mallocr.c (mALLOc, rEALLOCc, mEMALIGn): Set errno
to ENOMEM on failure.
------------------------------------------------------------------------
Index: libc/stdlib/mallocr.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdlib/mallocr.c,v
retrieving revision 1.14
diff -u -p -r1.14 mallocr.c
--- libc/stdlib/mallocr.c 3 Jun 2005 18:57:30 -0000 1.14
+++ libc/stdlib/mallocr.c 6 Oct 2005 20:00:47 -0000
@@ -268,6 +268,7 @@ extern "C" {
#include <stdio.h> /* needed for malloc_stats */
#include <limits.h> /* needed for overflow checks */
+#include <errno.h> /* needed to set errno to ENOMEM */
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
@@ -2341,7 +2342,10 @@ Void_t* mALLOc(RARG bytes) RDECL size_t
/* Check for overflow and just fail, if so. */
if (nb > INT_MAX || nb < bytes)
+ {
+ errno = ENOMEM;
return 0;
+ }
MALLOC_LOCK;
@@ -2804,7 +2808,10 @@ Void_t* rEALLOc(RARG oldmem, bytes) RDEC
/* Check for overflow and just fail, if so. */
if (nb > INT_MAX || nb < bytes)
+ {
+ errno = ENOMEM;
return 0;
+ }
#if HAVE_MMAP
if (chunk_is_mmapped(oldp)) @@ -3037,7 +3044,10 @@ Void_t*
mEMALIGn(RARG alignment, bytes)
/* Check for overflow. */
if (nb > INT_MAX || nb < bytes)
+ {
+ errno = ENOMEM;
return 0;
+ }
m = (char*)(mALLOc(RCALL nb + alignment + MINSIZE));