birnbaum@paul.rutgers.edu (Rich Birnbaum) (01/03/91)
Running lint on this program produces no errors:
main()
{
int i;
if (0)
i=0;
printf ("%d",i);
}
Shouldn't it complain that 'i' may be used before set? I could have sworn it
did at some point in my life!
This is a VERY pared down version of a bigger program.
I tried this on a Sun, a Gould, and a Pyramid.
Maybe lint isn't as smart as I thought...
Please email responses to birnbaum@pica.army.mil.
jtc@van-bc.wimsey.bc.ca (J.T. Conklin) (01/03/91)
In article <Jan.2.12.52.08.1991.467@paul.rutgers.edu> birnbaum@paul.rutgers.edu (Rich Birnbaum) writes: >Running lint on this program produces no errors: > >main() >{ > int i; > if (0) > i=0; > printf ("%d",i); >} > >Shouldn't it complain that 'i' may be used before set? I could have sworn it >did at some point in my life! It should, it doesn't, and ou've probably seen the 'i' may be used before set mesage before. Rather than doing data flow analysis, lint raises this warning only if the first use of a variable is access rather than assignment. I then tried FlexeLint by Gimpel Software. FlexeLint is a commercial lint which I highly recommend --- I was _sure_ that it would catch the bug. I was sadly disappointed. I looked in the manual and the description for warning 530 explained that it too uses the "first seen" algorithm. I'm going to write a letter to gimpel urging them to do better, as this is the first blemish I've discovered in an otherwise excelent product. Still, FlexeLint is a bit more thourough that most other lints, so I thought I'd include a comparison of output. lint: test.c ============== (4) warning: constant in conditional context (7) warning: main() returns random value to invocation environment ============== function returns value which is always ignored printf flexelint: FlexeLint (U32) Ver. 4.00F, Copyright Gimpel Software 1985-1990 --- Module: test.c _ if (0) test.c 4 Warning 506: Constant value Boolean _ printf ("%d",i); test.c 6 Info 718: printf undeclared, assumed to return int test.c 6 Info 746: call to printf not made in the presence of a prototype _ } test.c 7 Warning 533: Return mode of main inconsistent with line 1 --- Global Wrap-up Warning 526: printf (line 6, file test.c) not defined Warning 628: no argument information provided for function printf (line 6, file test.c) Having a good lint always helps, but like seatbelts, even those of us with the best intentions forget to use them. Having a compiler with good diagnostics is a big plus too. Just for kicks, let's see what gcc gives us: gcc -O -Wall: test.c: In function main: test.c:2: warning: return-type defaults to `int' test.c:6: warning: implicit declaration of function `printf' test.c:3: warning: `i' may be used uninitialized in this function test.c:7: warning: control reaches end of non-void function Pretty good :-) --jtc -- J.T. Conklin Toolsmith, Language Lawyer ...!{uunet,ubc-cs}!van-bc!jtc, jtc@wimsey.bc.ca
dolf@idca.tds.PHILIPS.nl (Dolf Grunbauer) (01/03/91)
In article <1131@van-bc.wimsey.bc.ca> jtc@van-bc.wimsey.bc.ca (J.T. Conklin) writes: >In article <Jan.2.12.52.08.1991.467@paul.rutgers.edu> birnbaum@paul.rutgers.edu (Rich Birnbaum) writes: >>Running lint on this program produces no errors: >> >>main() >>{ >> int i; >> if (0) >> i=0; >> printf ("%d",i); >>} >> >>Shouldn't it complain that 'i' may be used before set? I could have sworn it >>did at some point in my life! > >It should, it doesn't, and ou've probably seen the 'i' may be used >before set mesage before. Rather than doing data flow analysis, lint >raises this warning only if the first use of a variable is access >rather than assignment. > >[DELETED] Shouldn't it also give a warning/error message like: "statement not reached" ? -- _ _ / U | Dolf Grunbauer Tel: +31 55 433233 Internet dolf@idca.tds.philips.nl /__'< Philips Information Systems UUCP ...!mcsun!philapd!dolf 88 |_\ If you are granted one wish do you know what to wish for right now ?