[PATCH] set errno for malloc failures

Jeff Johnston jjohnstn@redhat.com
Fri Oct 7 19:52:00 GMT 2005


Modified patch checked in.

-- Jeff J.

Jeff Johnston wrote:

> Hi Bob,
>
>  Thanks for the patch.  Some changes are required because the 
> functions in mallocr are reentrant when built with INTERNAL_NEWLIB set 
> on.  This means that the errno of the passed in reentrancy struct must 
> be used to set errno in these situations.
>
>  I'll modify the patch appropriately.
>
> -- Jeff J.
>
> Bob Wilson wrote:
>
>> 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));
>>
>>  
>>
>



More information about the Newlib mailing list