cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: myrealloc() invites mem leaks: not a fix but a mistake

From: Pete Wilson <pete142_at_yahoo.com>
Date: Sat, 19 Dec 2009 05:38:08 -0800 (PST)

Please allow me to retract this poorly-thought-out "fix," which breaks the realloc() interface. Of course it's up to the caller to save his old pointer, just as with realloc().

Very sorry for the noise.

-- Pete

--- On Sat, 12/19/09, Pete Wilson <pete142_at_yahoo.com> wrote:

> From: Pete Wilson <pete142_at_yahoo.com>
> Subject: myrealloc() invites mem leaks
> To: curl-library_at_cool.haxx.se
> Date: Saturday, December 19, 2009, 5:35 AM
> Very elementary fix, but I get
> nervous every time I look at myrealloc().
>
> It would be a good idea to defend against the mem leak
> lurking in myrealloc(): if realloc() fails, it returns NULL,
> destroying the ptr passed to it. It's better to save the
> caller's ptr before calling realloc(), returning that saved
> ptr if realloc() fails:
>
> void *
> myrealloc ( void *ptr, size_t size ) {
>   // There might be a realloc() out there that doesn't
> like reallocing
>   //   NULL pointers, so we take care
> of it here
>   void *save_ptr = ptr;
>   if(ptr) {
>     ptr = realloc( ptr, size
> );   // try realloc(); return new ptr
>     return ptr ? ptr : save_ptr;  // 
> on success, else old ptr
>   }
>   else
>     return malloc( size );
> }
>
> -- Pete

      
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2009-12-19