Re: Style question: Should one check for NULL pointers?

Alan Stern
2003-07-11T15:03:14Z

On Fri, 11 Jul 2003, Eli Carter wrote:

> > Not really needed, since a segfault will produce almost as much
> > information as a BUG_ON(). Certainly it will produce enough to let a
> > developer know that the pointer was NULL.
>
> Your first message said, "I see no reason for pure paranoia,
> particularly if it's not commented as such." A BUG_ON() call makes it
> clear that the condition should never happen. Dereferencing a NULL
> leaves the question of whether NULL is an unhandled case or invalid
> input. BUG_ON() is an explicit paranoia check, and with a bit of
> preprocessing magic, you could compile out all of those checks.
>
> So it documents invalid input conditions, allows you to eliminate the
> checks in the name of speed or your personal preference, or use them to
> help with debugging/testing.

Okay, that makes sense. Particularly the debugging and testing part. And
for an excellent example of _documented_ paranoia, see the source to
schedule_timeout().

But if you look very far through the kernel sources you will see many
occurrences of code similar to this:

static void release(struct xxx *ptr)
{
if (!ptr)
return;
...

I can't see any reason for keeping something like that.

Alan Stern