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 <[email protected]> wrote: > From: Pete Wilson <[email protected]> > Subject: myrealloc() invites mem leaks > To: [email protected] > 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
