Page 1 of 1

Why Warning 661

Posted: Mon Jan 06, 2020 6:15 pm
by SeeCwriter
With the following test class

Code: Select all

class myclass 
{
public: 
  int count;
  buf[4000];
  myclass(): count(0) {buf[0]=0;}
  void RemoveLast();
}
 
myclass::RemoveLast()
{
  if ( count > 1 ) buf[count - 1] = 0; <-- Warning 661
}
I don't understand why I'm getting waring 661:

Code: Select all

packet.cpp  369  warning 661: potential out of bounds pointer access: excess of 
    0:2147479647 bytes
  if ( count > 1 ) buf[count - 1] = 0;
                   ^    ~~~~~~~~

Re: Why Warning 661

Posted: Thu Jan 09, 2020 2:50 pm
by mmetivier
Likely because Lint believes that your "count" could be anything in range of a signed int. While your check ensures that it doesn't use anything that will cause an access off the "left" of the buf, that still leaves Lint with the belief that the range could be anything from [1:2147483647]. So, if count were, in fact, 2147483647, you'd be 2147479647 elements off the end of the array, hence the warning.

You could resolve this by letting Lint know that you are aware of this by either expanding your conditional, so that " && count < 4000", or using assert to verify the same thing.