Bug 240456 - realloc(x, 0) should not return NULL
Summary: realloc(x, 0) should not return NULL
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: Konstantin Belousov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-09-09 19:44 UTC by Ed Maste
Modified: 2020-06-04 16:29 UTC (History)
2 users (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 freebsd_triage 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 freebsd_triage 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 freebsd_triage 2019-09-13 09:21:14 UTC
https://reviews.freebsd.org/D21632
Comment 4 commit-hook freebsd_committer freebsd_triage 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
Comment 5 commit-hook freebsd_committer freebsd_triage 2019-09-24 06:20:39 UTC
A commit references this bug:

Author: kib
Date: Tue Sep 24 06:19:41 UTC 2019
New revision: 352644
URL: https://svnweb.freebsd.org/changeset/base/352644

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

  PR:	240456

Changes:
_U  stable/12/
  stable/12/contrib/jemalloc/src/jemalloc.c
Comment 6 commit-hook freebsd_committer freebsd_triage 2019-09-24 06:23:45 UTC
A commit references this bug:

Author: kib
Date: Tue Sep 24 06:23:39 UTC 2019
New revision: 352646
URL: https://svnweb.freebsd.org/changeset/base/352646

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

  PR:	240456

Changes:
_U  stable/11/
  stable/11/contrib/jemalloc/src/jemalloc.c