From: Ian Campbell Subject: Re: [PATCH V7 4/7] libxl, Introduce libxl__realloc. Date: Thu, 21 Jul 2011 08:49:28 +0100 Message-ID: <1311234568.32010.21.camel@zakaz.uk.xensource.com> References: <1311197072-16003-1-git-send-email-anthony.perard@citrix.com> <1311197072-16003-5-git-send-email-anthony.perard@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1311197072-16003-5-git-send-email-anthony.perard@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Anthony PERARD Cc: Xen Devel , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org On Wed, 2011-07-20 at 22:24 +0100, Anthony PERARD wrote: > Signed-off-by: Anthony PERARD > --- > tools/libxl/libxl_internal.c | 20 ++++++++++++++++++++ > tools/libxl/libxl_internal.h | 1 + > 2 files changed, 21 insertions(+), 0 deletions(-) > > diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c > index e259278..527ebc5 100644 > --- a/tools/libxl/libxl_internal.c > +++ b/tools/libxl/libxl_internal.c > @@ -102,6 +102,26 @@ void *libxl__calloc(libxl__gc *gc, size_t nmemb, size_t size) > return ptr; > } > > +void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size) > +{ > + void *new_ptr = realloc(ptr, new_size); On failure realloc will return NULL but not free the old pointer, so I think in that case you will set alloc_ptrs[i] to NULL but not actually free the old pointer, hence leaking it. I think you can just check for new_ptr == NULL and return NULL up front. Normally that would leak the old ptr but by leaving it in the gc array we avoid that pitfall. If new_size==0 then realloc behaves as free. I reckon you can just return NULL then too and allow the gc to clean up. Or you could outlaw such uses in this interface and abort(), that seems harsh however. BTW libxl__free_all does cope correctly with NULLs mid-way through alloc_ptrs[] which did concern me initially. Ian. > + int i = 0; > + > + if (ptr == NULL) { > + libxl__ptr_add(gc, new_ptr); > + } else if (new_ptr != ptr) { > + for (i = 0; i < gc->alloc_maxsize; i++) { > + if (gc->alloc_ptrs[i] == ptr) { > + gc->alloc_ptrs[i] = new_ptr; > + break; > + } > + } > + } > + > + > + return new_ptr; > +} > + > char *libxl__sprintf(libxl__gc *gc, const char *fmt, ...) > { > char *s; > diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h > index 4580be9..64942e6 100644 > --- a/tools/libxl/libxl_internal.h > +++ b/tools/libxl/libxl_internal.h > @@ -145,6 +145,7 @@ _hidden int libxl__ptr_add(libxl__gc *gc, void *ptr); > _hidden void libxl__free_all(libxl__gc *gc); > _hidden void *libxl__zalloc(libxl__gc *gc, int bytes); > _hidden void *libxl__calloc(libxl__gc *gc, size_t nmemb, size_t size); > +_hidden void *libxl__realloc(libxl__gc *gc, void *ptr, size_t new_size); > _hidden char *libxl__sprintf(libxl__gc *gc, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3); > _hidden char *libxl__strdup(libxl__gc *gc, const char *c); > _hidden char *libxl__dirname(libxl__gc *gc, const char *s);