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

Richard B. Johnson (root@chaos.analogic.com)
Fri, 11 Jul 2003 10:52:50 -0400 (EDT)


On Fri, 11 Jul 2003, David D. Hagood wrote:

> Hua Zhong wrote:
>
> > Not always true. In some cases you know how to handle: just return
> > without doing anyting.
>
> That is NOT an error condition - the API specifically allows NULL to be
> passed in, and specifically states that no action will be taken in that
> case.
>
> But consider the following code:
>
> sscanf(0,0);
>
> That IS an error condition - both the string to scan and the format
> string are NULL. In this case sscanf should return EITHER 0 (no items
> matched) or better still -1 (error).
>

But it does neither. Instead, it seg-faults your code!

The problem lies with the original question. The question
referred to "Style" (look at the subject-line). It is
not a question about style, but a question about utility
and design. Style has nothing to do with it.

If you are writing code for an embedded system, the code
must always run even if RAM got trashed from alpha particles
or EMP. In that case, you trap every possible condition and
force a restart off a hardware timer, refreshing everything in
RAM from PROM or NVRAM.

If you are writing code to examine the contents of sys_errlist[],
prior to adding another error-code, then you don't check anything
and it's file-name is probably a.out, compiled from xxx.c.

So, the extent to which one checks for exceptions and provides
utility for handling exceptions depends upon the code design, not
it's style.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.20 on an i686 machine (797.90 BogoMips).
Why is the government concerned about the lunatic fringe? Think about it.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/