[Freeciv-Dev] Re: [patches] freeing NULL ptrs
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
> >> PS. Would it be worth it to create a fc_free that ignores all NULL
> >> pointers passed to it?
> >
> > I thought ANSI C requires that free(NULL) is a no-op? K&R2 implies
> > this. Are there any compilers that we care about that behave
> > differently?
>
> http://groups.google.com/groups?hl=en&safe=off&th=b640de69b11cf7dc,3
Instead of "ignore", how about "warns about"? I try to be very picky
about my heap maintenance, and freeing a NULL -- however legal -- is a
sign that something isn't right somewhere (IMHO). What about this:
void somefunc() {
char *foo;
int bar[10];
int i;
foo = malloc(16);
/* ... */
for(i = 0;i <= 10;i++) /* off-by-one */
bar[i] = 0;
/* ... */
free(foo); /* Potentially a no-op */
}
And, yes, I know this *exact* example will still work "OK" (at least it
did so on solaris 8/gcc 2.95.2 and linux 2.4.6/gcc 2.96, gcc pads the
stack), but here's the concept in a few lines. If I free something, I
expect it to be non-NULL. If it is NULL, I want to know about it. It
could be a sign of heap corruption, buffer overflows, or other undesired
side-effects. If I know the line number and file where this free-ing
happens, I can go and check if NULL is really a valid pointer value.
Otherwise I'd never know ...
-jdm
Department of Computer Science, Duke University, Durham, NC 27708-0129
Email: justin@xxxxxxxxxxx
- [Freeciv-Dev] Re: [patches] freeing NULL ptrs, (continued)
- [Freeciv-Dev] Re: [patches] freeing NULL ptrs, Justin Moore, 2001/08/17
- [Freeciv-Dev] Re: [patches] freeing NULL ptrs, Kevin Brown, 2001/08/17
- [Freeciv-Dev] Re: [patches] freeing NULL ptrs, Raimar Falke, 2001/08/19
- [Freeciv-Dev] Re: [patches] freeing NULL ptrs, Ross W. Wetmore, 2001/08/19
- [Freeciv-Dev] Re: [patches] freeing NULL ptrs, Raimar Falke, 2001/08/19
- [Freeciv-Dev] Re: [patches] freeing NULL ptrs, Kevin Brown, 2001/08/19
- [Freeciv-Dev] Re: [patches] freeing NULL ptrs, Justin Moore, 2001/08/19
- [Freeciv-Dev] Re: [patches] freeing NULL ptrs, Raimar Falke, 2001/08/19
[Freeciv-Dev] Re: [patches] freeing NULL ptrs, David Pfitzner, 2001/08/17
|
|