From: solar Date: Wed, 14 Jun 2006 04:13:51 +0000 (+0000) Subject: Fixed size==0 bug pointed out by Rod P. X-Git-Tag: v0.5~162 X-Git-Url: https://pd.if.org/git/?p=pdclib;a=commitdiff_plain;h=32d523defc53a80f1e15e977a4c7a616acc464c3 Fixed size==0 bug pointed out by Rod P. --- diff --git a/functions/stdlib/realloc.c b/functions/stdlib/realloc.c index c1ba38f..c2bebd4 100644 --- a/functions/stdlib/realloc.c +++ b/functions/stdlib/realloc.c @@ -16,25 +16,28 @@ void * realloc( void * ptr, size_t size ) { + void * newptr = NULL; if ( ptr == NULL ) { return malloc( size ); } + if ( size > 0 ) { - struct _PDCLIB_memnode_t * baseptr = (struct _PDCLIB_memnode_t *)( (char *)ptr - sizeof( struct _PDCLIB_memnode_t ) ); - if ( baseptr->size >= size ) - { - return ptr; - } - else - { - void * newptr = malloc( size ); - memcpy( newptr, ptr, baseptr->size ); - free( ptr ); - return newptr; - } + struct _PDCLIB_memnode_t * baseptr = (struct _PDCLIB_memnode_t *)( (char *)ptr - sizeof( struct _PDCLIB_memnode_t ) ); + if ( baseptr->size >= size ) + { + /* Current memnode is large enough; nothing to do. */ + return ptr; + } + else + { + /* Get larger memnode and copy over contents. */ + newptr = malloc( size ); + memcpy( newptr, ptr, baseptr->size ); + } } - return NULL; + free( ptr ); + return newptr; } #endif