Re: fork() Problem?

Richard B. Johnson (root@chaos.analogic.com)
Wed, 5 May 1999 14:09:04 -0400 (EDT)


On Wed, 5 May 1999, Steve Dodd 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!
>
> Not if the code is written in C, which it seems to be.
>
> It'll be true if the result of fork() is true, or false otherwise. pid will
> be assigned the result of fork() in either case. As fork() returns 0 (false)
> in the child, and the PID of the child in the parent (true), what's the
> problem?[1]
>
> S.
>
> [1] Assuming fork() doesn't fail of course, which is reasonable in test code.
>

The problem is that if anybody writes code like this, they should be
terminated with extreme prejudice. This is using a gcc compiler bug as
a feature.

#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;
}

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

gcc 'fixes' obvious coding errors like this. If a compiler fixes
these errors, it should fix the source while it's at it so I don't
have to see them again.

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/