#4390 closed enhancement (fixed)

Make zero size allocation result consistent across directives

Reported by: Sebastian Huber Owned by: Sebastian Huber
Priority: normal Milestone: 6.1
Component: admin Version: 6
Severity: normal Keywords: qualification
Cc: Blocked By:
Blocking:

Description

There is a special case in malloc() which results in a malloc( 0 ) == NULL invariant. This special case should move to rtems_heap_allocate_aligned_with_boundary() to have a consistent behaviour across the API level memory allocation directives.

Change History (7)

comment:1 Changed on 04/20/21 at 18:37:24 by Sebastian Huber

Summary: Make zero size allocation result consistent accross directivesMake zero size allocation result consistent across directives

comment:2 Changed on 04/20/21 at 22:41:58 by Joel Sherrill

This is an area of undefined behavior per POSIX. It might be worth considering having this be a debug failure.

https://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html

comment:3 Changed on 04/21/21 at 04:43:41 by Sebastian Huber

It is not undefined behaviour in the latest POSIX edition:

https://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html

It is implementation-defined behaviour. The implementation has two options. The RTEMS malloc( 0 ) returns NULL. Linux seems to return a unique pointer.

The rtems_memalign(&p, 8, 0) returns a unique pointer. I think this size == 0 behaviour should be consistent in RTEMS.

comment:4 Changed on 05/04/21 at 11:08:10 by Sebastian Huber <sebastian.huber@…>

Resolution: fixed
Status: assignedclosed

In d692c62d/rtems:

Make zero size allocation result consistent

The zero size allocations had no consistent behaviour in RTEMS. For
example, malloc( 0 ) returned NULL and posix_memalign( &p, align, 0 )
returned in p a unique pointer (or NULL if no memory is available). In
POSIX, zero size memory allocations are implementation-defined
behaviour. The implementation has two options:

https://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html

https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.html

Linux and FreeBSD return a unique pointer for zero size memory
allocations. Use this approach for RTEMS as well throughout the memory
allocation directives

Close #4390.

comment:5 Changed on 05/06/21 at 13:15:09 by Sebastian Huber <sebastian.huber@…>

In 2c5199b/rtems:

Return NULL for zero size allocations

In POSIX, zero size memory allocations are implementation-defined
behaviour. The implementation has two options:

https://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html

https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.html

Linux and FreeBSD return a unique pointer for zero size memory
allocations. Return NULL on RTEMS to more likely catch the use of a
zero size memory area by erroneous applications.

Update #4390.

comment:6 Changed on 05/07/21 at 05:08:44 by Sebastian Huber <sebastian.huber@…>

In c46d125/rtems:

Check the alignment in posix_memalign() earlier

Make sure all conditions to do a proper memory allocation are satisfied
before a zero size memory allocation is performed.

Update #4390.

comment:7 Changed on 06/23/21 at 07:07:55 by Sebastian Huber

Keywords: qualification added
Note: See TracTickets for help on using tickets.