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

            bug685.cpp

1     #include <stdio.h>
2
3     unsigned a[] = { 1, 2, 3, 4, 5, 0xFFFF };
4
5     unsigned sum( unsigned *p )
6         {
7         unsigned s = 0;
8
9         while( (*p & 0xFF) <= 255 )
10            {
11            s += *p++ & 0xFF;
12            }
13        return s;
14        }
15
16    int main()
17        {
18        printf( "sum = %d\n", sum(a) );
19        return 0;
20        }

Our programmer is summing up an array whose termination is signalled by a special sentinel. However it's taking a lot longer than anticipated. What's the problem?


bug685.cpp lint Output

--- Module:   bug685.cpp
                              _
    while( (*p & 0xFF) <= 255 )
bug685.cpp(9) : Warning 685: Relational operator '<=' always evaluates to
    'true'
    _
    }
bug685.cpp(14) : Info 818: Pointer parameter 'p' (line 5) could be declared as
    pointing to const

Reference Manual Explanation

 
685   Relational operator 'String,' always evaluates to 'String' 
      -- The first String is one of '>', '>=', '<' or '<=' and
      identifies the relational operator.  The second string is
      one of 'True' or 'False'.  The message is given when an
      expression is compared to a constant and the precision of
      the expression indicates that the test will always succeed or
      always fail.  For example,

                char ch;
                ...
                if( ch >= -128 ) ...

      In this example, the precision of char ch is 8 bits signed
      (assuming the fcu flag has been left in the OFF state) and
      hence it has a range of values from -128 to 127 inclusive.
      Hence the test is always True.

      Note that, technically, ch is promoted to int before
      comparing with the constant.  For the purpose of this
      comparison we consider only the underlying precision.  As
      another example, if u is an unsigned int then

                if( (u & 0xFF) > 0xFF ) ...

      will also raise message 685 because the expression on the
      left hand side has an effective precision of 16 bits.

If you have comments or questions about this bug, please post them to our Discussion Forum

Previous Bug - Bug #684 - July 2002

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

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