Subject: Re: NULL return value checking
To: None <tech-kern@netbsd.org>
From: Matthew Mondor <mmondor@gobot.ca>
List: tech-kern
Date: 04/22/2002 03:47:45
On 23 Apr 2002 14:10:18 -0700
cgd@broadcom.com wrote:

> > Try to avoid assignments inside if-conditions. For example, don't
> > write this: 
> > 
> > if ((foo = (char *) malloc (sizeof *foo)) == 0)
> >   fatal ("virtual memory exhausted");
> > 
> > instead, write this: 
> > 
> > foo = (char *) malloc (sizeof *foo);
> > if (foo == 0)
> >   fatal ("virtual memory exhausted");
> 
> (Sure, that's not _actually_ a check against NULL, but it's an
> explicit test separate from the assignment, with the same intent an
> effect as a check against "NULL".)
> 

char **foo;

This would originally have been in my case:

if(! (foo = malloc(sizeof(*foo))) )
    fatal("virtual memory exhausted");

which would become, following /usr/share/misc/style:

if( (foo = malloc(sizeof(*foo))) ==NULL)
    fatal("virtual memory exhausted");



and

if( row[0] && row[1] && row[2] && row[3] ) {
}

becoming

if( row[0]!=NULL && row[1]!=NULL && row[2]!=NULL && row[3]!=NULL ) {
}

or

if(
    row[0] != NULL
    && row[1] != NULL
    && row[2] != NULL
    && row[3] != NULL
) {
}

In this particular later case the first form would be more readable and is much less typing