[PATCH] D83301: [clang-tidy] More strict on matching the standard memset function in memset-usage check.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 8 06:28:09 PDT 2020


hokein updated this revision to Diff 276405.
hokein added a comment.

add parameterCountIs(3).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D83301/new/

https://reviews.llvm.org/D83301

Files:
  clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.cpp
@@ -75,3 +75,8 @@
   // despite v == 0.
   memset(p, -1, v);
 }
+
+void *memset(int);
+void NoCrash() {
+  memset(1);
+}
Index: clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp
@@ -20,11 +20,19 @@
 namespace bugprone {
 
 void SuspiciousMemsetUsageCheck::registerMatchers(MatchFinder *Finder) {
-  // Note: void *memset(void *buffer, int fill_char, size_t byte_count);
+  // Match the standard memset:
+  // void *memset(void *buffer, int fill_char, size_t byte_count);
+  auto MemsetDecl =
+      functionDecl(hasName("::memset"),
+                   parameterCountIs(3),
+                   hasParameter(0, hasType(pointerType(pointee(voidType())))),
+                   hasParameter(1, hasType(isInteger())),
+                   hasParameter(2, hasType(isInteger())));
+
   // Look for memset(x, '0', z). Probably memset(x, 0, z) was intended.
   Finder->addMatcher(
       callExpr(
-          callee(functionDecl(hasName("::memset"))),
+          callee(MemsetDecl),
           hasArgument(1, characterLiteral(equals(static_cast<unsigned>('0')))
                              .bind("char-zero-fill")),
           unless(
@@ -36,14 +44,14 @@
 
   // Look for memset with an integer literal in its fill_char argument.
   // Will check if it gets truncated.
-  Finder->addMatcher(callExpr(callee(functionDecl(hasName("::memset"))),
+  Finder->addMatcher(callExpr(callee(MemsetDecl),
                               hasArgument(1, integerLiteral().bind("num-fill")),
                               unless(isInTemplateInstantiation())),
                      this);
 
   // Look for memset(x, y, 0) as that is most likely an argument swap.
   Finder->addMatcher(
-      callExpr(callee(functionDecl(hasName("::memset"))),
+      callExpr(callee(MemsetDecl),
                unless(hasArgument(1, anyOf(characterLiteral(equals(
                                                static_cast<unsigned>('0'))),
                                            integerLiteral()))),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83301.276405.patch
Type: text/x-patch
Size: 2526 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200708/89e25624/attachment-0001.bin>


More information about the cfe-commits mailing list