Click on image to see enlargment

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

            bug845.cpp

1    typedef unsigned Property;
2    #define PRIME    0x01
3    #define NEGATIVE 0x02
4    #define ZERO     0x04
5
6    Property int_check( int k )
7        {
8        Property p = 0;
9        if( k < 0 ) p |= NEGATIVE;
10       else if( k == 0 ) p &= ZERO;
11       else for( int i = 2; ; i++ )
12           {
13           if( i * i > k )
14               {
15               p |= PRIME;
16               break;
17               }
18           else if( k % i == 0 )
19               break;
20           }
21       return p;
22       }

A programmer wrote int_check(k) to determine some of the properties of its integer argument. But there's a slight flaw. Can you spot it?


bug845.cpp lint Output

--- Module:   bug845.cpp (C++)
                           _
    else if( k == 0 ) p &= ZERO;
bug845.cpp  10  Info 845: The left argument to operator '&' is certain to be 0
    [Reference: file bug845.cpp: line 8]

Reference Manual Explanation


845    The [left/right] argument to operator 'Name' is certain to be 0 
       -- An operand that can be deduced to always be 0 has been
       presented to an arithmetic operator in a context that arouses
       suspicion.  The name of the operator is provided in the message
       as well as the side of the operator (left or right) that had the
       unusual value.  For example:

                 n = 0;
                 k = m & n;

       will produce a message that the right hand operand of operator
       '&' is certain to be zero.

       The operands examined are the right hand sides of operators

                 + - | ||

       the left hand sides of operators

                 / %

       and both sides of operators

                 * & << >> &&

       The reason that the left hand side of operator + (and friends) is
       not examined for zero is that zero is the identity operation for
       those operators and hence is often used as an initializing value.
       For example:

                 sum = 0;
                 for( ... )
                     sum = sum + what_ever;       // OK, no message

       The message is not issued for arithmetic constant zeros.  Info
       835 in Section 13.4 is issued in that instance.

       The message is also suspended when the expression has
       side-effects.  For example:

                 i = 0;
                 buf[i++] = 'A';

       We don't consider it reasonable to force the programmer to write:

                 buf[0] = 'A';
                 i = 1;


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


Previous Bug - Bug #616 - April 2010

Use our Interactive Demo to Run FlexeLint on our Bugs of the Month

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2010, Gimpel Software, All rights reserved.