[lkml]   [2003]   [Jul]   [11]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: Style question: Should one check for NULL pointers?
Followup to:  <>
By author: Eli Carter <>
In newsgroup:
> >
> > 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.

... but it also bloats the code, in this case, in many ways
needlessly. You don't want to compile out all BUG_ON()'s, just the
ones that wouldn't be checked for anyway.

In fact, have a macro that explicitly tests for nullness by
dereferencing a pointer might be a good idea; on most architectures it
will be a lot cheaper than BUG_ON() (which usually requires an
explicit test), and the compiler at least has a prayer at optimizing
it out.

<> at work, <> 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
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

 \ /
  Last update: 2005-03-22 13:46    [W:0.741 / U:0.008 seconds]
©2003-2017 Jasper Spaans. hosted at Digital OceanAdvertise on this site