如何训练覆盖率进行"suspicious sizeof"或SIZEOF_MISMATCH发现?
How to train Coverity for "suspicious sizeof" or SIZEOF_MISMATCH finding?
我有一个模板函数,其专用化可以执行零化:
template <class T>
void SecureWipeBuffer(T *buf, size_t n)
{
volatile T *p = buf+n;
while (n--)
*((volatile T*)(--p)) = 0;
}
...
template <>
void SecureWipeBuffer(word64* p, size_t n)
{
asm volatile("rep stosq" : "+c"(n), "+D"(p) : "a"(0) : "memory");
}
Coverity正在产生一个关于SecureWipeBuffer
的发现:
word64 val;
...
SecureWipeBuffer(&val, 1);
调查结果是:
>>> CID 164713: Incorrect expression (SIZEOF_MISMATCH)
>>> Passing argument "&val" of type "word64 *" and argument "1UL" to function "SecureWipeBuffer" is suspicious because "sizeof (word64)" /*8*/ is expected.
275 SecureWipeBuffer(&val, 1);
如何训练SecureWipeBuffer
的覆盖率需要元素计数,而不是字节计数?
编辑:我们在Windows代码中发现了两个类似的发现。此外,Coverity正在对标准库功能进行发现。就好像它没有意识到C++处理元素计数,而不是字节计数。
以下是来自Microsft标准库代码的<xmemory>
25 if (_Count == 0)
26 ;
27 else if (((size_t)(-1) / sizeof (_Ty) < _Count)
CID 12348 (#1 of 1): Wrong sizeof argument (SIZEOF_MISMATCH)
suspicious_sizeof: Passing argument _Count * 4U /* sizeof (std::allocator<void *>::value_type) */
to function operator new which returns a value of type std::allocator<void *>::value_type is suspicious.
28 || (_Ptr = ::operator new(_Count * sizeof (_Ty))) == 0)
29 _Xbad_alloc(); // report no memory
我找到了这个Github,它试图通过这样做来抑制它*:
std::fill_n(out, spec_.width_ - 1, fill);
out += spec_.width_ - 1;
} else if (spec_.align_ == ALIGN_CENTER) {
// coverity[suspicious_sizeof]
out = writer_.fill_padding(out, spec_.width_, 1, fill);
} else {
std::fill_n(out + 1, spec_.width_ - 1, fill);
在 Coverity Prevention 中的 Silentencing 误报中也建议这样做,此处介绍了另一种方法:Coverity SA - 不包括 boost、stlport 错误。
*我不确定这是否是你想要的,但这就是我得到的!
相关文章:
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 同一对象的"sizeof"的不同答案
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- 为什么 sizeof 在 C++ 中给出不正确的字节数?
- sizeof(size_t) 可以小于 sizeof(int) 吗?
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 为什么 sizeof(ar)/ sizeof(ar[0]) 在传递给函数时无法在向量上正常工作?
- 为什么 MSVC _count_of实现将 0 添加到 sizeof 的结果中?
- alignas() 对 sizeof() 的影响 - 强制性的?
- 在C++中,运算符 sizeof 返回什么数据类型?
- sizeof 函数如何在带和不带位字段的结构上工作?(填充)
- s.length 和 sizeof(a) 有什么区别?
- sizeof(enum) 可以不同于 sizeof(std::underlying_type<Enum>::
- 如何将数组传递给使用 sizeof 的函数?
- sizeof(size_t) 和 sizeof(ptrdiff_t) 什么时候会有所不同?
- " sizeof "操作员在编程中真的很重要吗,尤其是在构建大型应用程序时?
- 在QScopedPointer的实现中使用sizeof
- Sizeof返回的是指针大小,而不是数组大小.有其他方法可以找到尺寸吗
- 为什么sizeof函数在这里不能正常工作
- "Operator mismatch" C++ 中的错误