Why Warning 661

PC-lint Plus User Discussion
Post Reply
SeeCwriter

Why Warning 661

Post by SeeCwriter » Mon Jan 06, 2020 6:15 pm

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;
                   ^    ~~~~~~~~

mmetivier

Re: Why Warning 661

Post by mmetivier » Thu Jan 09, 2020 2:50 pm

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.

Post Reply