ゼロ化を実行する特殊化を備えたテンプレート関数があります。
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
バイト数ではなく要素数を取るCoverity をトレーニングするにはどうすればよいですか?
編集: Windows コードで 2 つの同様の調査結果を見つけました。さらに、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