#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 Apr 20, 2021 at 6:37:24 PM by Sebastian Huber

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

comment:2 Changed on Apr 20, 2021 at 10:41:58 PM 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 Apr 21, 2021 at 4:43:41 AM 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 May 4, 2021 at 11:08:10 AM 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 May 6, 2021 at 1:15:09 PM 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 May 7, 2021 at 5:08:44 AM 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 Jun 23, 2021 at 7:07:55 AM by Sebastian Huber

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