题
Klocwork 报告错误:-
“ ABR - 缓冲区溢出,'OIDSP'的数组索引可能超出范围。尺寸64的数组“ OIDSP”可能使用索引值-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
IS: - (顺便说一句,对于-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;
}
有任何想法吗?
解决方案
我可能会遗漏一些东西,但是您的函数(check_index_lower_legality)不会修改“len”变量,也不会从用于访问数组的函数返回,因此您提供的代码片段似乎会正确生成运行时缓冲区下溢(对于 len < 0 的值)。如果您认为该报告确实不正确,您能否扩展该示例?
谢谢,格温。
其他提示
这是错误的错误。您需要添加额外的检查来告诉 len 始终 > 1。
因此,您可以通过添加完全不需要的 if 条件来跳过此错误。
if (check_index_lower_legality (len,-1))
{
if(len > 1)
oidsp[len-1] = specProb;
}
或者您可以将此错误标记为误报并再次运行 klockworks。很可能在下一份报告中跳过这一点。
我认为 Klocwork 无法遵循这种逻辑。您需要告诉它 check_index_lower_legality 的行为是这样的。
不隶属于 StackOverflow