PC-lint/FlexeLint Output | Reference Manual Explanation | Home Page

            bug568.cpp

1     #include <stdio.h>
2     #include <string.h>
3
4     unsigned a[100] = {0};
5
6     int main()
7         {
8         char buf[200];
9         unsigned n = 0;
10
11        while( fgets( buf, 200, stdin ) )
12            {
13            if( n < 100 ) a[n++] =  strlen(buf);
14            }
15        while( --n >= 0 )
16            {
17            printf( "%d\n", a[n] );
18            }
19        return 0;
20        }

Our programmer wanted to print the lengths of some lines from standard in but the program is behaving erratically. What's going on?


bug568.cpp lint Output

--- Module:   bug568.cpp
                    _
    while( --n >= 0 )
bug568.cpp(15) : Warning 685: Relational operator '>=' always evaluates to
    'true'
bug568.cpp(15) : Warning 568: non-negative quantity is never less than zero
                              _
        printf( "%d\n", a[n] );
bug568.cpp(17) : Info 817: Conceivably negative subscript (-1) in operator '['
    [Reference: file bug568.cpp: lines 9, 15, 17]

Reference Manual Explanation

 
568   nonnegative quantity is never less than zero.  --
      Comparisons of the form:

                u >= 0      0 <= u
                u <  0      0 >  u

      are suspicious if u is an unsigned quantity or a quantity
      judged to be never less then 0.  See also message  775.

Previous Bug - Bug #1729 - March 2002

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2003, Gimpel Software