curl-library
myrealloc() invites mem leaks
Date: Sat, 19 Dec 2009 02:35:22 -0800 (PST)
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