Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 20 Aug 1998 22:23:43 -0700
From:      Don Lewis <Don.Lewis@tsc.tdk.com>
To:        hackers@FreeBSD.ORG
Subject:   Re: Realloc fix for review
Message-ID:  <199808210523.WAA08503@salsa.gv.tsc.tdk.com>

next in thread | raw e-mail | index | archive | help
On Aug 20, 10:19am, Warner Losh wrote:
} Subject: Realloc fix for review
} 
} Recently, OpenBSD went through their source tree and fixed all
} instances of
} 	a = realloc(a, size);
} with
} 	na = realloc(a, size);
} 	if (!na)
} 		free(a);
} 	a = na;
} 
} So rather than hack some really complicated a and na's, I write a new
} function, added it to libc and then modified all of src/lib to use
} this new function.  I called it frealloc (freeing realloc).  I added
} it to the man page, etc.  I know that this isn't in ANSI C nor in
} POSIX, but after seeing how hard it was to get people to use strncpy
} right, I thought it would be good to add this to libc.

I'd worry about causing name space pollution.  You might break someone
else's code who has their own function called frealloc ...


On Aug 20,  4:48pm, Dag-Erling Coidan =?iso-8859-1?Q?Sm=F8rgrav?= wrote:
} Subject: Re: Realloc fix for review

} Is that really a good idea? If you free the old block when realloc()
} fails, you lose whatever data was in it (and therefore potentially
} lose the ability to generate a sensible error message or recover
} gracefully). Such a change should be done on a per-case basis, rather
} than blindly applied to every snippet that calls realloc().

The original code also loses access to the data and leaks core because
it overwrites a even if realloc() failed.  That is, unless you kept another
copy of the pointer somewhere, but that would be bad because that pointer
might be pointing to some bogus location in the heap if realloc succeeded.

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199808210523.WAA08503>