Bug 240456 - realloc(x, 0) should not return NULL
Summary: realloc(x, 0) should not return NULL
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-standards mailing list
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-09-09 19:44 UTC by Ed Maste
Modified: 2019-09-17 18:37 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ed Maste freebsd_committer 2019-09-09 19:44:34 UTC
In the same way that malloc(0) returns non-NULL, realloc(x, 0) should return a pointer to an allocation of size 0, not NULL.

test case:

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
        printf("%p\n", realloc(malloc(1), 0));
}

% ./a.out        
0x0

Twitter thread for reference: https://twitter.com/RichFelker/status/1171108501751554048
Comment 2 Ed Maste freebsd_committer 2019-09-09 20:01:22 UTC
glibc bug report with additional detail: https://sourceware.org/bugzilla/show_bug.cgi?id=12547
Comment 3 Konstantin Belousov freebsd_committer 2019-09-13 09:21:14 UTC
https://reviews.freebsd.org/D21632
Comment 4 commit-hook freebsd_committer 2019-09-17 18:37:16 UTC
A commit references this bug:

Author: kib
Date: Tue Sep 17 18:36:30 UTC 2019
New revision: 352456
URL: https://svnweb.freebsd.org/changeset/base/352456

Log:
  realloc(x, 0) should not return NULL.

  See http://www.open-std.org/jtc1/sc22/wg14/www/docs/summary.htm#dr_400.
  Upstream jemalloc issue is opened by emaste at
  https://github.com/jemalloc/jemalloc/issues/1629.

  Reviewed by:	emaste
  PR:	240456
  Sponsored by:	The FreeBSD Foundation
  MFC after:	1 week
  DIfferential revision:	https://reviews.freebsd.org/D21632

Changes:
  head/contrib/jemalloc/src/jemalloc.c