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
See: http://www.open-std.org/jtc1/sc22/wg14/www/docs/summary.htm#dr_400
glibc bug report with additional detail: https://sourceware.org/bugzilla/show_bug.cgi?id=12547
https://reviews.freebsd.org/D21632
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
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
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