glibc's realloc(3) is broken. It was originally good (I believe) until at some point, when it was changed to conform to C89, which had a bogus specification that required that it returns NULL. C99 fixed the mistake from C89, and so glibc's realloc(3) is non-conforming to C99/C11/POSIX.1-2008. C17 broke again the definition of realloc(3). Link: <https://github.com/shadow-maint/shadow/pull/1095> Link: <https://nabijaczleweli.xyz/content/blogn_t/017-malloc0.html> Link: <https://inbox.sourceware.org/libc-alpha/5gclfbrxfd7446gtwd2x2gfuquy7ukjdbrndphyfmfszxlft76@wwjz7spd4vd7/T/#t> Co-developed-by: наб <nabijaczleweli@nabijaczleweli.xyz> Signed-off-by: наб <nabijaczleweli@nabijaczleweli.xyz> Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com> Acked-by: Paul Eggert <eggert@cs.ucla.edu> Signed-off-by: Alejandro Colomar <alx@kernel.org>
42 lines
824 B
C
42 lines
824 B
C
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
#ifndef SHADOW_INCLUDE_LIB_ALLOC_REALLOCF_H_
|
|
#define SHADOW_INCLUDE_LIB_ALLOC_REALLOCF_H_
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stddef.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "attr.h"
|
|
|
|
|
|
#define REALLOCF(p, n, type) \
|
|
( \
|
|
_Generic(p, type *: (type *) reallocarrayf(p, (n) ?: 1, sizeof(type)))\
|
|
)
|
|
|
|
|
|
ATTR_ALLOC_SIZE(2, 3)
|
|
ATTR_MALLOC(free)
|
|
inline void *reallocarrayf(void *p, size_t nmemb, size_t size);
|
|
|
|
|
|
inline void *
|
|
reallocarrayf(void *p, size_t nmemb, size_t size)
|
|
{
|
|
void *q;
|
|
|
|
q = reallocarray(p, nmemb ?: 1, size ?: 1);
|
|
|
|
if (q == NULL)
|
|
free(p);
|
|
return q;
|
|
}
|
|
|
|
|
|
#endif // include guard
|