#1730 closed defect (wontfix)

Change malloc(0) behavior to match FreeBSD and Linux

Reported by: Sebastian Huber Owned by: Joel Sherrill
Priority: normal Milestone: 4.11
Component: score Version: 4.11
Severity: normal Keywords:
Cc: ralf.corsepius@…, chrisj@…, dufault@…, gedare@…, Peer Stritzinger Blocked By:
Blocking:

Description

From C99 we have (section 7.20.3): If the size of the space requested is zero, the behavior is implementation- de?ned: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.

RTEMS currently returns NULL for malloc(0) due to some special case handling in malloc(). FreeBSD and Linux on the other hand return a non-NULL pointer in most cases. To ease the porting of applications we should adopt this approach and remove the special case in malloc() and delegate the allocation of a zero size to the heap implementation. The current heap implementation returns a non-NULL pointer in this case unless the heap is completely used.

Attachments (2)

malloc-zero.patch (1.2 KB) - added by Sebastian Huber on 01/21/11 at 07:05:36.
Patch.
malloc-zero_v1.patch (808 bytes) - added by Sebastian Huber on 01/21/11 at 07:07:41.
Patch.

Download all attachments as: .zip

Change History (10)

Changed on 01/21/11 at 07:05:36 by Sebastian Huber

Attachment: malloc-zero.patch added

Patch.

Changed on 01/21/11 at 07:07:41 by Sebastian Huber

Attachment: malloc-zero_v1.patch added

Patch.

comment:1 Changed on 01/21/11 at 07:07:41 by Sebastian Huber

attachments.isobsolete: 01

comment:2 Changed on 01/21/11 at 07:14:30 by Ralf Corsepius

Cc: Ralf Corsepius added

comment:3 Changed on 01/25/11 at 12:20:42 by dufault

Cc: dufault added

comment:4 Changed on 01/27/11 at 07:24:08 by Peer Stritzinger

Cc: Peer Stritzinger added

comment:5 Changed on 01/28/11 at 15:56:42 by Gedare Bloom

Cc: giddyup44@… added

comment:6 Changed on 02/16/11 at 08:16:10 by Sebastian Huber

Resolution: wontfix
Status: newclosed

comment:7 Changed on 03/01/11 at 06:27:29 by Chris Johns

Cc: Chris Johns added

Replying to comment:2:

I am very opposed to this change and don't see any reason to change something.

I support Ralf and oppose any change.

I checked the FreeBSD man page and the malloc code. FreeBSD allows malloc to be configured via /etc/malloc.conf at the system level and also via the environment using MALLOC_OPTIONS. The 'V' option causes malloc to return a NULL pointer rather than the default behaviour of allocating a 1 byte. Any code in FreeBSD that cannot handle both cases is broken. Also the C FAQ covers the topic with http://c-faq.com/ansi/malloc0.html. I agree with the comment in the FAQ.

comment:8 Changed on 11/24/14 at 18:58:28 by Gedare Bloom

Version: HEAD4.11

Replace Version=HEAD with Version=4.11 for the tickets with Milestone >= 4.11

Note: See TracTickets for help on using tickets.