diff --git a/clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp b/clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp --- a/clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp +++ b/clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp @@ -80,7 +80,7 @@ auto IsRecordSizeOf = expr(sizeOfExpr(hasArgumentOfType(equalsBoundNode("Record")))); auto ArgChecker = [&](Matcher RecordConstraint, - BindableMatcher SecondArg) { + BindableMatcher SecondArg = expr()) { return allOf(argumentCountIs(3), hasArgument(0, IsStructPointer(RecordConstraint, true)), hasArgument(1, SecondArg), hasArgument(2, IsRecordSizeOf)); @@ -89,8 +89,7 @@ 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( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -156,6 +156,9 @@ ` when `sizeof(...)` is compared against a `__int128_t`. +- Made :doc:`cert-oop57-cpp ` more sensitive + by checking for an arbitrary expression in the second argument of `memset`. + - Improved :doc:`cppcoreguidelines-prefer-member-initializer ` check. diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp b/clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp @@ -88,3 +88,17 @@ 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 +}