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

Richard B. Johnson
2003-07-11T14:37:56Z

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.