Problems with malloc(LARGEINT) How to fix?

Christopher Faylor
Mon Mar 11 14:00:00 GMT 2002

I've just noticed that malloc becomes confused when given an argument
that is near the maximum for an unsigned int.

An attempt is made to round up the number of bytes allocated to some
value -- which is not possible when you're near the boundary of what
is available for a signed integer.

The result is that a request to allocate a large number is translated
into a small request rather than returning NULL.

Looking at the newest version of Doug Lea's malloc, it seems like he
is handling this by doing a REQUEST_OUT_OF_RANGE check.  I'm not sure
what the best way to handle this would be, however.  I guess newlib
should do the same thing but that's hard to do with the current
macro that is used to handle these types of things.

Any suggestions on how to deal with this problem?

The macro which returns the rounded number of bytes is below.


#define request2size(req) \
 (((unsigned long)((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) < \
   (((unsigned long) (req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) & ~(MALLOC_ALIGN_MASK)))

More information about the Newlib mailing list