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

Dennis Bliefernicht
2003-07-10T20:49:45Z

Alan Stern wrote:
> On the other hand, what if on rare occasions the pointer actually is NULL,
> even though it's not supposed to be? This can only be the result of an
> error somewhere else in the kernel (such as incorrect locking during a
> data structure update). Detecting the NULL pointer and returning an error
> code will hide the existence of the true underlying error. But if the
> check _isn't_ made, then as soon as the pointer is derefenced there will
> be a nice big segfault. This will immediately alert people to the
> existence of a problem, something they otherwise might not be aware of at
> all.
The problem is IMHO code where some pretty fragile things are handled,
especially file systems. I'd say: DO the paranoia checks if some fragile
things are involved like key structures of the file system that can take
_permanent_ damage. If you check for a NULL pointer you still have the
chance to properly leave the system in a consistent state and no user
will be happy if his filesystem goes messy just because someone saved a
check to have nicer code, even if the original of the NULL pointer
wasn't his fault, even if it's a developing version. So if the check
isn't a total performace disaster, do it whenever permanent damage could
occur.
On other sections where, let's say just a user memory allocation would
crash, checks could be ommitted, because it isn't that fatal and leaves
no permanent destruction.

Just my opinion :)
TriPhoenix