diff --git a/lib/alloc/realloc.h b/lib/alloc/realloc.h index fd31ab89..63f14dc2 100644 --- a/lib/alloc/realloc.h +++ b/lib/alloc/realloc.h @@ -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))) \ ) diff --git a/lib/alloc/reallocf.h b/lib/alloc/reallocf.h index 5854330a..2d328b6a 100644 --- a/lib/alloc/reallocf.h +++ b/lib/alloc/reallocf.h @@ -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; }