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

Alan Stern
2003-07-13T21:27:18Z

On Sat, 12 Jul 2003, Horst von Brand wrote:

> Alan Stern <stern@rowland.harvard.edu> said:
>
> [...]
>
> > 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.
>
> Just like free(3)

NO! Not just like free(). The documentation for free() explicitly states
that NULL pointers are valid input and result in no action. A
release()-type function, by contrast, is called back from core system code
that guarantees it will always pass a pointer to the currently-registered
owner of the corresponding resource. If the owner were NULL, the
release() function wouldn't have been called in the first place.

Alan Stern