test-skeleton.c (xrealloc): Support realloc-as-free

Message ID 4b83d238-aedc-f8c9-c324-35871ddaffc9@redhat.com
State Committed
Headers

Commit Message

Florian Weimer June 23, 2016, 12:54 p.m. UTC
  On 06/23/2016 02:45 PM, Mike Frysinger wrote:
> On 23 Jun 2016 14:23, Florian Weimer wrote:
>> If the requested size is zero, realloc returns NULL, but the
>> deallocation is still successful.
>
> not exactly.  realloc(NULL, 0) will return a valid pointer (much
> like malloc(NULL) will).  code relies on this to generate cookies.

Ugh, right.  At least our malloc behaves in this way.  What about the 
attached patch?

Thanks,
Florian
  

Comments

Mike Frysinger June 23, 2016, 1:18 p.m. UTC | #1
On 23 Jun 2016 14:54, Florian Weimer wrote:
> On 06/23/2016 02:45 PM, Mike Frysinger wrote:
> > On 23 Jun 2016 14:23, Florian Weimer wrote:
> >> If the requested size is zero, realloc returns NULL, but the
> >> deallocation is still successful.
> >
> > not exactly.  realloc(NULL, 0) will return a valid pointer (much
> > like malloc(NULL) will).  code relies on this to generate cookies.
> 
> Ugh, right.  At least our malloc behaves in this way.  What about the 
> attached patch?

OK
-mike
  

Patch

test-skeleton.c (xrealloc): Support realloc-as-free

If the requested size is zero, realloc returns NULL, but the
deallocation is still successful, unless the pointer is also
NULL, when realloc behaves as malloc (0).

2016-06-23  Florian Weimer  <fweimer@redhat.com>

	* test-skeleton.c (xrealloc): Support deallocation with n == 0.

diff --git a/test-skeleton.c b/test-skeleton.c
index 0be4af1..d9bf989 100644
--- a/test-skeleton.c
+++ b/test-skeleton.c
@@ -109,10 +109,10 @@  __attribute__ ((unused))
 static void *
 xrealloc (void *p, size_t n)
 {
-  p = realloc (p, n);
-  if (p == NULL)
+  void *result = realloc (p, n);
+  if (result == NULL && (n > 0 || p == NULL))
     oom_error ("realloc", n);
-  return p;
+  return result;
 }
 
 /* Write a message to standard output.  Can be used in signal