Re: linux-kernel-digest V1 #3762

Matt Stocum (mstocum@adelphia.net)
Thu, 06 May 1999 18:34:20 -0400


> From: "Richard B. Johnson" <root@chaos.analogic.com>
> Date: Wed, 5 May 1999 17:53:37 -0400 (EDT)
> Subject: Re: fork() Problem?
> Again, after the assignment was made, there was no check of the
> value. This is what Lint picked up. When you read the standard,
> it talks about the value of the left operand. It also talks about
> its type because of the promotion rules, that's all your cited
> text shows.
>
> Here we have a compound statement in which an assignment is
> being made, i.e.,
>
> left = right;
>
> Then we have a conditional expression which tests the result of
> an operation:
>
> if(left = right)
>
> As stated, and as Lint picked up, we are now testing if the assignment
> occurred, not the value of the left-most operand. Therefore, we have
> to make the assignment first:
>

Explain how the assignment can fail.

> (left = right) ... Parenthesis for priority.
>
> then test the result:
>
> if ( (left = right) ) ... exactly as Lint has shown.

Lint is still wrong. if(left = right) and if((left = right)) are
EXACTLY the same statements. now if(left = right == 0) and if((left =
right) == 0) are different statements (I have no idea what the first
would do anyway). In the first statement there is no need for
parenthesis because there are only two operands and = takes exactly two
operands. You can put in ()s up the wazoo and it will make no
difference.

>
> Lint shows that even this is an error because the result is
> not boolean. It really should be.
>

I think you are missing a common point about Lint. It's a STYLE
checker. It reports errors in STYLE. There are lots of things in C
that are legal that should never be done. left = right ? printf("Hi\n")
: printf("Bye\n"); is legal, it's just very ugly and I'm sure Lint would
report it as an error.

> if ( (left = right) == 0 )
>
> The result of '==' is boolean which 'if' tests. It is, however,
> common usage to do:

C doesn't have a boolean type. In "boolean" expressions it evaluates 0
to false, and non-zero to true.

>
> if(value)
>
> because there usually is a functional equivalent.
>

First off Lint isn't the definitive standard for C. The C standard is,
which clearly states that this is 100% legal. Second, -Wall doesn't
report errors, it reports warnings. And the warning reported is a
sugestion for clarity when humans read the code, not for the compiler.

> Not only does Lint pick up this common error, but so does gcc if
> you turn on all the error reporting with -Wall.
>
> xxx.c: In function `main':
> xxx.c:13: warning: suggest parentheses around assignment used as truth value
>
> Cheers,
> Dick Johnson

-matt

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