RE: fork() Problem?

Manuel A. McLure (mmt@Unify.Com)
Wed, 5 May 1999 14:22:57 -0700


You have your test wrong. If the compiler *DOES* show the string, then it is
buggy. By DEFINITION, in C, 0 is FALSE, and the value of an assignment is
the value assigned to the variable.

if (i = foo())

is a common construct, equivalent to

if ((i = foo()) != 0)

You can discuss all you want whether the construct is ugly or misleading,
but this is not "taking advantage of a gcc bug", it is taking advantage of a
C design feature.

--
Manuel A. McLure KE6TAW <mmt@unify.com>

-----Original Message----- From: Richard B. Johnson [mailto:root@chaos.analogic.com] Sent: Wednesday, May 05, 1999 11:09 AM To: Steve Dodd Cc: linux-kernel@vger.rutgers.edu Subject: Re: fork() Problem?

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/

- 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/