ERROR in libc/stdlib/mallocr.c???

Corinna Vinschen
Thu Aug 16 04:10:00 GMT 2001


I think I have found an error in libc/stdlib/mallocr.c.

Look into line 1384ff:

    #define SIZE_SZ                (sizeof(INTERNAL_SIZE_T))
    #define MALLOC_ALIGN           8
    #define MALLOC_ALIGNMENT       (SIZE_SZ + SIZE_SZ)

As you can see, MALLOC_ALIGNMENT is by default defined as 2*SIZE_SZ
which in turn is defined as sizeof(INTERNAL_SIZE_T) which in turn is
defined in line 431ff:

    #ifndef INTERNAL_SIZE_T
    #define INTERNAL_SIZE_T size_t

The problem here is that according to the comment in line 168ff
MALLOC_ALIGNMENT is set to 8 by default:

  MALLOC_ALIGNMENT          (default: NOT defined)
     Define this to 16 if you need 16 byte alignment instead of 8 byte alignment
     which is the normal default.

Unfortunately this isn't true due to the above statement

     #define MALLOC_ALIGNMENT       (SIZE_SZ + SIZE_SZ)

Imagine a system which has sizeof(size_t) = 2 bytes. MALLOC_ALIGNMENT
is now set to 4!

As a result of that error, any allocation of small values (<=8) fails
in mALLOc() at line 2545ff:

    malloc_extend_top(RCALL nb);
    remainder_size = long_sub_size_t(chunksize(top), nb);
    if (chunksize(top) < nb || remainder_size < (long)MINSIZE)
      return 0; /* propagate failure */

Even if `malloc_extend_top' succeeded, the following if fails
since `remainder_size' is < 0.

Odd enough, MALLOC_ALIGNMENT must be >= 8, otherwise the implementation
always fails to do the right thing.

What can we do? I can see three obvious solutions:

- Force ports to targets with sizeof(size_t) < 4 to define
  MALLOC_ALIGNMENT as a value >= 8.

- Force ports to targets with sizeof(size_t) < 4 to set INTERNAL_SIZE_T
  to a datatype with at least 4 bytes (not good, IMO).

- The define in line 1387 could be changed to

The third is the most simple solution which result in a correct
behaviour, IMO.


Corinna Vinschen
Cygwin Developer
Red Hat, Inc.

More information about the Newlib mailing list