[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: proper realloc(p,0) behavior?
From: |
Jim Meyering |
Subject: |
Re: proper realloc(p,0) behavior? |
Date: |
Thu, 24 Mar 2011 20:02:53 +0100 |
Paul Eggert wrote:
> On 03/24/2011 10:17 AM, Eric Blake wrote:
>> How should gnulib react? And are there any existing GNU programs that
>> would break if C99 realloc semantics were enforced?
>
> My kneejerk reaction is that
> programs should be written so as to be portable to
> both C89 (which GNU implements) and C99 realloc.
> Programs that rely on GNU behavior but get C99 behavior
> will have memory leaks, but that's better than crashing.
> GNU programs should not be written to rely on C99 behavior,
> because they may crash.
>
> I don't offhand know of any existing GNU code that expects C99
> behavior and would crash with GNU behavior.
>
> The issue is well worth documenting in the gnulib manual.
>
> It should be fairly easy to write code that is portable
> to both C89 and C99 and does not leak, by using free (p) rather
> than realloc (p, 0). For example, we could apply the following
> patch to xmalloc.c:
>
> --- a/lib/xmalloc.c
> +++ b/lib/xmalloc.c
> @@ -52,10 +52,18 @@ xmalloc (size_t n)
> void *
> xrealloc (void *p, size_t n)
> {
> - p = realloc (p, n);
> - if (!p && n != 0)
> - xalloc_die ();
> - return p;
> + if (n)
> + {
> + p = realloc (p, n);
> + if (!p)
> + xalloc_die ();
> + return p;
> + }
> + else
> + {
> + free (p);
> + return NULL;
> + }
> }
>
> /* If P is null, allocate a block of at least *PN bytes; otherwise,
Good idea.
Maybe write it like this instead?
if (n == 0)
{
free (p);
return NULL;
}
p = realloc (p, n);
if (!p)
xalloc_die ();
return p;
-------------
or like this?
-------------
if (n == 0)
{
free (p);
p = NULL
}
else
{
p = realloc (p, n);
if (!p)
xalloc_die ();
}
return p;
FYI, I searched for examples with a literal 0, using this:
http://codesearch.google.com/codesearch?sa=N&q=realloc\+*\%28[^,],\+*0\%29%3B
This is the only significant hit:
daikon/kvasir/valgrind/massif/tests/realloc.c - 5 identical
16: x = realloc(x, 0); // equivalent to free(x), and ends up
17: // calling Valgrind's (and Massif's)
free
pag.csail.mit.edu/daikon/download/daikon.tar.gz - GPL - C
There are plenty of others, but they're in configure tests.
- proper realloc(p,0) behavior?, Eric Blake, 2011/03/24
- Re: proper realloc(p,0) behavior?, Paul Eggert, 2011/03/24
- Re: proper realloc(p,0) behavior?,
Jim Meyering <=
- Re: proper realloc(p,0) behavior?, Eric Blake, 2011/03/24
- Re: proper realloc(p,0) behavior?, Paul Eggert, 2011/03/24
- Re: proper realloc(p,0) behavior?, Paolo Bonzini, 2011/03/25
- Re: proper realloc(p,0) behavior?, Eric Blake, 2011/03/25
- Re: proper realloc(p,0) behavior?, Paolo Bonzini, 2011/03/25
- Re: proper realloc(p,0) behavior?, Eric Blake, 2011/03/25
- Re: proper realloc(p,0) behavior?, Paul Eggert, 2011/03/25
- Re: proper realloc(p,0) behavior?, Eric Blake, 2011/03/25
- [PATCH] xmalloc: revert yesterday's regression, Eric Blake, 2011/03/25
- Re: proper realloc(p,0) behavior?, Paul Eggert, 2011/03/25