Complete.Org: Mailing Lists: Archives: freeciv-dev: August 2001:
[Freeciv-Dev] Re: [patches] freeing NULL ptrs
Home

[Freeciv-Dev] Re: [patches] freeing NULL ptrs

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Gaute B Strokkenes <gs234@xxxxxxxxx>
Cc: David Pfitzner <dwpfitzner@xxxxxxxxx>, <freeciv-dev@xxxxxxxxxxx>
Subject: [Freeciv-Dev] Re: [patches] freeing NULL ptrs
From: Justin Moore <justin@xxxxxxxxxxx>
Date: Sat, 18 Aug 2001 01:15:07 -0400 (EDT)

> >> 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



[Prev in Thread] Current Thread [Next in Thread]