Alejandro Colomar wrote in <https://sourceware.org/pipermail/libc-alpha/2024-October/160645.html>: > - glibc, in 2006/2007, made a so-called bugfix change to conform to > C89, which effectively made realloc(p,0) non-conforming to C99. This > happened in the following commit: > > commit 11bf311edc76f5ddc469a8c396e313e82d76be15 > Author: Ulrich Drepper <drep...@redhat.com> > Date: Thu Jan 11 21:51:07 2007 +0000 > > [BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ > #3480, BZ #3483, BZ # > 3493, BZ #3514, BZ #3515, BZ #3664, BZ #3673, BZ #3674] > > [...] > > 2006-12-08 Ulrich Drepper <drep...@redhat.com> > * malloc/memusage.c: Handle realloc with new size of zero > and > non-NULL pointer correctly. > > [...]
This statement is incorrect. I tested the behaviour of realloc (ptr, 0) on a glibc 2.3.2 from 2003 (Fedora 1), and it is the same as the behaviour of recent glibcs. The commit above affects only the realloc override in libmemusage.so, which normal programs rarely use. Bruno
/* Program that shows how realloc (p, 0) behaves. */ #include <stdlib.h> #include <stdio.h> #include <errno.h> int main () { char *p1 = malloc (200); errno = 0; char *q1 = realloc (p1, 0); int err1 = errno; char *p2 = malloc (300); errno = 0; char *q2 = realloc (p2, 0); int err2 = errno; if (q1 == NULL && err1 == 0 && q2 == NULL && err2 == 0) printf ("(A)\n"); else if (q1 == NULL && err1 != 0 && q2 == NULL && err2 != 0) printf ("(B)\n"); else if (q1 != NULL && q2 != NULL && q1 == q2) printf ("(C)\n"); else if (q1 != NULL && q2 != NULL && q1 != q2) printf ("(D)\n"); else printf ("Unknown:\n%p %p %d\n%p %p %d\n", p1, q1, err1, p2, q2, err2); } /* glibc 2022 (A) glibc 2003 (A) musl (D) macOS (D) FreeBSD (D) NetBSD (D) OpenBSD (D) AIX (A) Solaris (D) Cygwin (D) mingw (A) MSVC (A) Android (A) */