Click on image to see enlargment

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

            bug647.cpp

1    #include <stdio.h>
2
3    int no_of_trees()
4        { /* compute, compute */ return 105000; }
5
6    int leaves_per_tree()
7        { /* compute some more */ return 25000; }
8
9    long total()
10       { return no_of_trees() * leaves_per_tree(); }
11
12   int main()
13       {
14       printf( "there are %ld leaves in the forest\n",
15               total() );
16       }

Wanting to exploit the added precision of 64-bit integral arithmetic, a forester wrote the above program to determine the actual number of leaves in his forest. But he's getting some funny results. What's going on? Note, he is using the LP64 model wherein longs and pointers are 64 bits. To run this example on PC-lint or FlexeLint you will need the option -sl8.


bug647.cpp lint Output

--- Module:   bug647.cpp (C++)
                                              _
    { return no_of_trees() * leaves_per_tree(); }
bug647.cpp(10) : Warning 647: Suspicious truncation

Reference Manual Explanation


647    Suspicious truncation   -- This message is issued when it appears
       that there may have been an unintended loss of information during
       an operation involving int or unsigned int the result of which is
       later converted to long.  It is issued only for systems in which
       int is smaller than long.  For example:

             (long) (n << 8)

       might elicit this message if n is unsigned int, whereas

             (long) n << 8

       would not.  In the first case, the shift is done at int precision
       and the high order 8 bits are lost even though there is a
       subsequent conversion to a type that might hold all the bits.  In
       the second case, the shifted bits are retained.

       The operations that are scrutinized and reported upon by this
       message are:  shift left, multiplication, and bit-wise
       complementation.  Addition and subtraction are covered by
       Informational message 776.

       The conversion to long may be done explicitly with a cast as
       shown or implicitly via assignment, return, argument passing or
       initialization.
       The message can be suppressed by casting.  You may cast one of
       the operands so that the operation is done in full precision as
       is given by the second example above.  Alternatively, if you
       decide there is really no problem here (for now or in the
       future), you may cast the result of the operation to some form of
       int.  For example, you might write:

           (long) (unsigned) (n << 8)

       In this way PC-lint/FlexeLint will know you are aware of and
       approve of the truncation.


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


Previous Bug - Bug #646 - September 2007

[ new ] 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 2007, Gimpel Software, All rights reserved.