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

H. Peter Anvin
2003-07-10T22:02:01Z

Followup to: <3F0DD3FD.3030403@triphoenix.de>
By author: Dennis Bliefernicht <itsme.nospam@triphoenix.de>
In newsgroup: linux.dev.kernel
>
> 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.
>

Actually, you have it somewhat backwards.

In most cases, checking for NULL pointers (and returning an error
whatnot) is actually *more* likely to cause permanent damage than
having the kernel bomb out. At least with the kernel bombing out you
won't keep grinding on a filesystem for which your kernel
datastructures are bad. This is *IMPORTANT*.

-hpa
--
<hpa@transmeta.com> at work, <hpa@zytor.com> in private!
If you send me mail in HTML format I will assume it's spam.
"Unix gives you enough rope to shoot yourself in the foot."
Architectures needed: ia64 m68k mips64 ppc ppc64 s390 s390x sh v850 x86-64