This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
[PATCH] set errno for malloc failures
- From: Bob Wilson <bwilson at tensilica dot com>
- To: newlib at sources dot redhat dot com
- Date: Thu, 06 Oct 2005 13:06:41 -0700
- Subject: [PATCH] set errno for malloc failures
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));