c - ABR - Klocwork 错误警报和错误处理

标签 c klocwork

Klocwork 报告错误:-

"ABR – Buffer overflow, array index of 'oidsp' may be out of bounds. Array 'oidsp' of size 64 may use index value(s) -2..-1."

对于这一行:-

if (check_index_lower_legality (len,-1))
{
oidsp[len-1] = specProb;
}

当 check_index_lower_legality 为:-

bool check_index_lower_legality (int index, int offset)
/**
 * This function checks that the index with the offset isn't 
 * below zero. 
 * If it is - returns 0 ;
 * If isn't - returns 1 ;
 **/
{

if (  (index + offset )<0) {
   return 0;
  }
 return 1 ; 
}

但是,当 check_index_lower_legality 为:- (顺便说一句,这是一个不正确的答案,对于 -2 或 -1 的偏移值,运行时会出现真正的错误。

bool check_index_lower_legality (int index, int offset)
/**
 * This function checks that the index with the offset isn't 
 * below zero. 
 * If it is - returns 0 ;
 * If isn't - returns 1 ;
 **/
{
 if (index <=0) {
  return 0;
 }
 return 1;
}

有什么想法吗?

最佳答案

这是错误的错误。您需要添加额外的检查来告诉 len 始终 > 1。

因此,您可以通过添加完全不需要的 if 条件来跳过此错误。

if (check_index_lower_legality (len,-1)) 
{
if(len > 1) 
oidsp[len-1] = specProb; 
} 

或者您可以将此错误标记为误报并再次运行 klockworks。很可能在下一份报告中跳过这一点。

关于c - ABR - Klocwork 错误警报和错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3253875/

相关文章:

c# - klocwork 错误 : No permission set for resource 'streamWriter' before accessing it

c - 标准输入中的文件结尾

c - 循环不会中断。 C程序

c - 这个for循环的答案怎么是5?

c - 不是空终止字符串误报?

c - 对于 64 位操作系统,内存泄漏是否仍然相关?

c++ - 当浮点值包含逗号而不是点时,如何读取该值?

c++ - Klocwork 9 与 Klocwork 12

c - 理解Klocwork按位运算符号