]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/alloc/realloc*.h: Always reallocate at least 1 byte
authorAlejandro Colomar <alx@kernel.org>
Wed, 16 Oct 2024 18:31:25 +0000 (20:31 +0200)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Tue, 22 Oct 2024 08:53:06 +0000 (10:53 +0200)
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>
lib/alloc/realloc.h
lib/alloc/reallocf.h

index fd31ab89e0c8dc3b2e72b5ddb545f5e81c11eba4..63f14dc2ecca8373865f9c33ff29c56726213160 100644 (file)
@@ -13,7 +13,7 @@
 
 #define REALLOC(p, n, type)                                                   \
 (                                                                             \
-       _Generic(p, type *:  (type *) reallocarray(p, n, sizeof(type)))       \
+       _Generic(p, type *: (type *) reallocarray(p, (n) ?: 1, sizeof(type))) \
 )
 
 
index 5854330aae9dbaf2481d6ef87fe35652c8ae536f..2d328b6a4a8ab744c689b27aa7593e321d102660 100644 (file)
@@ -16,7 +16,7 @@
 
 #define REALLOCF(p, n, type)                                                  \
 (                                                                             \
-       _Generic(p, type *:  (type *) reallocarrayf(p, n, sizeof(type)))      \
+       _Generic(p, type *: (type *) reallocarrayf(p, (n) ?: 1, sizeof(type)))\
 )
 
 
@@ -30,10 +30,9 @@ reallocarrayf(void *p, size_t nmemb, size_t size)
 {
        void  *q;
 
-       q = reallocarray(p, nmemb, size);
+       q = reallocarray(p, nmemb ?: 1, size ?: 1);
 
-       /* realloc(p, 0) is equivalent to free(p);  avoid double free.  */
-       if (q == NULL && nmemb != 0 && size != 0)
+       if (q == NULL)
                free(p);
        return q;
 }