[clang-tidy] Extend cert-oop57-cpp to check non-zero memset values

Clang Tidy check cert-oop57-cpp now checks for arbitrary-valued
arguments in memset expressions containing non-trivially
default-constructible instances. Previously it only checked literal 0 values.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D126186
This commit is contained in:
Endre Fülöp 2022-05-22 23:28:10 +02:00
parent 57203af167
commit d33f199910
3 changed files with 19 additions and 3 deletions

View File

@ -80,7 +80,7 @@ void NonTrivialTypesLibcMemoryCallsCheck::registerMatchers(
auto IsRecordSizeOf =
expr(sizeOfExpr(hasArgumentOfType(equalsBoundNode("Record"))));
auto ArgChecker = [&](Matcher<CXXRecordDecl> RecordConstraint,
BindableMatcher<Stmt> SecondArg) {
BindableMatcher<Stmt> SecondArg = expr()) {
return allOf(argumentCountIs(3),
hasArgument(0, IsStructPointer(RecordConstraint, true)),
hasArgument(1, SecondArg), hasArgument(2, IsRecordSizeOf));
@ -89,8 +89,7 @@ void NonTrivialTypesLibcMemoryCallsCheck::registerMatchers(
Finder->addMatcher(
callExpr(callee(namedDecl(hasAnyName(
utils::options::parseListPair(BuiltinMemSet, MemSetNames)))),
ArgChecker(unless(isTriviallyDefaultConstructible()),
expr(integerLiteral(equals(0)))))
ArgChecker(unless(isTriviallyDefaultConstructible())))
.bind("lazyConstruct"),
this);
Finder->addMatcher(

View File

@ -156,6 +156,9 @@ Changes in existing checks
<clang-tidy/checks/bugprone-sizeof-expression>` when `sizeof(...)` is
compared against a `__int128_t`.
- Made :doc:`cert-oop57-cpp <clang-tidy/checks/cert-oop57-cpp>` more sensitive
by checking for an arbitrary expression in the second argument of `memset`.
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
<clang-tidy/checks/cppcoreguidelines-prefer-member-initializer>` check.

View File

@ -88,3 +88,17 @@ void baz(const NonTrivial &Other) {
mymemcmp(&Data, &Other, sizeof(Data));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: consider using comparison operators instead of calling 'mymemcmp'
}
void nonNullSetValue() {
NonTrivial Data;
// Check non-null-valued second argument.
std::memset(&Data, 1, sizeof(Data));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling 'memset' on a non-trivially default constructible class is undefined
}
void nonLiteralSetValue(char C) {
NonTrivial Data;
// Check non-literal second argument.
std::memset(&Data, C, sizeof(Data));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling 'memset' on a non-trivially default constructible class is undefined
}