Re: fork() Problem?

Richard B. Johnson (root@chaos.analogic.com)
Wed, 5 May 1999 16:41:57 -0400 (EDT)


On Wed, 5 May 1999, Kurt Garloff wrote:

> On Wed, May 05, 1999 at 09:52:23AM -0400, Richard B. Johnson wrote:
>
> > > if (pid = fork())
> > ^^^^________ logical test of an assignment? This will always
> > be true!
> > if((pid = fork()) != 0) or
> > if((pid = fork()))
> > You need to make the assignment first, then
> > test for non-zero.
>
> SORRY? I can't let this uncorrected!
>
> Why do you think it's legal to use smth like a = b = c; ???
>
> Because b = c return the value assigned to b.
>
>
> if (pid = fork()) do_smth;
>
> is identical to
>
> if ((pid = fork()) != 0) do_smth;
>

Not so. Your first instance tested if the assignment succeeded. It
is a common error called 'boolean test of assignment' and is the
most common error I've seen in 'C' coding.

The next instance makes the assignment first, then makes a boolean
test. This is actually another error which Lint will pick up. However,
since a boolean type is commonly 'int', but not always, it is
probably acceptable.

#include <stdio.h>

int foo()
{
return 0;
}

int main()
{
int i;
if(i=foo())
(void)puts("If this is not seen, it is a compiler BUG!");

return 0;
}

This is the output from Lint which was configured to allow most
'gcc' anomolies to get through.

LCLint 2.2a --- 04 Sep 96

xxx.c: (in function main)
xxx.c:13,5: Test expression for if is assignment expression: i = foo()
The condition test is an assignment expression. Probably, you mean to use ==
instead of =. If an assignment is intended, add an extra parentheses nesting
(e.g., if ((a = b)) ...) to suppress this message. (-predassign will suppress
message)
xxx.c:13,5: Test expression for if not bool, type int: i = foo()
Test expression type is not boolean or int. (-predboolint will suppress
message)

Finished LCLint checking --- 2 code errors found

Cheers,
Dick Johnson
***** FILE SYSTEM WAS MODIFIED *****
Penguin : Linux version 2.2.6 on an i686 machine (400.59 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.

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