Re: [hackers] [sbase][PATCH] Realloc variants should not fail if the requested size is zero

From: Dimitris Papastamos <sin_AT_2f30.org>
Date: Thu, 17 Mar 2016 09:58:31 +0000

On Thu, Mar 17, 2016 at 10:11:02AM +0100, Mattias Andrée wrote:
> Signed-off-by: Mattias Andrée <maandree_AT_kth.se>
> ---
> libutil/ealloc.c | 2 +-
> libutil/reallocarray.c | 2 +-
> 2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/libutil/ealloc.c b/libutil/ealloc.c
> index 320865d..5a3cce1 100644
> --- a/libutil/ealloc.c
> +++ b/libutil/ealloc.c
> _AT_@ -60,7 +60,7 @@ void *
> enrealloc(int status, void *p, size_t size)
> {
> p = realloc(p, size);
> - if (!p)
> + if (!p && size)
> enprintf(status, "realloc: out of memory\n");
> return p;
> }
> diff --git a/libutil/reallocarray.c b/libutil/reallocarray.c
> index c6e5219..b964626 100644
> --- a/libutil/reallocarray.c
> +++ b/libutil/reallocarray.c
> _AT_@ -43,7 +43,7 @@ ereallocarray(void *optr, size_t nmemb, size_t size)
> {
> void *p;
>
> - if (!(p = reallocarray(optr, nmemb, size)))
> + if (!(p = reallocarray(optr, nmemb, size)) && nmemb && size)
> eprintf("reallocarray: out of memory\n");
>
> return p;

This is implementation-defined behaviour, exiting is also valid. Don't
write code that relies on this idiom.

I am aware we have not documented this behaviour but that's minor in my
opinion.
Received on Thu Mar 17 2016 - 10:58:31 CET

This archive was generated by hypermail 2.3.0 : Thu Mar 17 2016 - 11:00:14 CET