fune/tools/clang-tidy/test/bugprone-suspicious-memset-usage.cpp
Chris Peterson 59cd83d07a Bug 1475882 - clang-tidy: Enable bugprone-suspicious-memset-usage check. r=andi
This check finds memset() calls with potential mistakes in their arguments. There are currently no bugprone-suspicious-memset-usage warnings in mozilla-central!

https://clang.llvm.org/extra/clang-tidy/checks/bugprone-suspicious-memset-usage.html

MozReview-Commit-ID: 9gmtidgMPwW

--HG--
extra : source : 9c6f6e1553c45751f476c2d8418b9d4735e27e50
extra : histedit_source : 721536e24babf3734e116bd6beaf8d43722cca96
2018-07-08 23:54:13 -07:00

22 lines
845 B
C++

// https://clang.llvm.org/extra/clang-tidy/checks/bugprone-suspicious-memset-usage.html
#include "structures.h"
void test(int* ip, char* cp)
{
// Case 1: Fill value is a character '0' instead of NUL '\0'.
memset(ip, '0', 1); // WARNING: suspicious for non-char pointers
memset(cp, '0', 1); // OK for char pointers
// Case 2: Fill value is truncated.
memset(ip, 0xabcd, 1); // WARNING: fill value gets truncated
memset(ip, 0x00cd, 1); // OK because value 0xcd is not truncated.
memset(ip, 0x00, 1); // OK because value is not truncated.
// Case 3: Byte count is zero.
memset(ip, sizeof(int), 0); // WARNING: zero length, potentially swapped
memset(ip, sizeof(int), 1); // OK with non-zero length
// See clang bug https://bugs.llvm.org/show_bug.cgi?id=38098
memset(ip, 8, 0); // OK with zero length without sizeof
}