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

Endre Fülöp via cfe-commits cfe-commits at lists.llvm.org
Tue May 31 23:23:51 PDT 2022


Author: Endre Fülöp
Date: 2022-06-01T08:23:23+02:00
New Revision: e290fcef111c24ed833c9768809eb900a21c94e0

URL: https://github.com/llvm/llvm-project/commit/e290fcef111c24ed833c9768809eb900a21c94e0
DIFF: https://github.com/llvm/llvm-project/commit/e290fcef111c24ed833c9768809eb900a21c94e0.diff

LOG: [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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/cert-oop57-cpp.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp b/clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp
index 8e6acea290826..5c8da5b4cacf7 100644
--- a/clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cert/NonTrivialTypesLibcMemoryCallsCheck.cpp
@@ -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(

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 5196c53291d19..c709f6d35e155 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -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.
 

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
index a5a38734ce6ae..880c5c3a1c9fe 100644
--- 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 @@ 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
+}


        


More information about the cfe-commits mailing list