[PATCH] D102714: [clang-tidy] Fix a crash on invalid code for memset-usage check.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 18 12:54:51 PDT 2021


hokein created this revision.
hokein added a reviewer: gribozavr2.
Herald added a subscriber: xazax.hun.
hokein requested review of this revision.
Herald added a project: clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102714

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


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.c
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.c
@@ -0,0 +1,11 @@
+// RUN: %check_clang_tidy  -expect-clang-tidy-error  %s bugprone-suspicious-memset-usage %t
+
+void *memset(void *, int, __SIZE_TYPE__);
+void *memset(void *);
+// CHECK-MESSAGES: :[[@LINE-1]]:7: error: conflicting types for 'memset'
+
+void test() {
+  // no crash
+  memset(undefine);
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: error: use of undeclared identifier
+}
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
@@ -32,7 +32,7 @@
   // Look for memset(x, '0', z). Probably memset(x, 0, z) was intended.
   Finder->addMatcher(
       callExpr(
-          callee(MemsetDecl),
+          callee(MemsetDecl), argumentCountIs(3),
           hasArgument(1, characterLiteral(equals(static_cast<unsigned>('0')))
                              .bind("char-zero-fill")),
           unless(hasArgument(
@@ -43,13 +43,13 @@
   // Look for memset with an integer literal in its fill_char argument.
   // Will check if it gets truncated.
   Finder->addMatcher(
-      callExpr(callee(MemsetDecl),
+      callExpr(callee(MemsetDecl), argumentCountIs(3),
                hasArgument(1, integerLiteral().bind("num-fill"))),
       this);
 
   // Look for memset(x, y, 0) as that is most likely an argument swap.
   Finder->addMatcher(
-      callExpr(callee(MemsetDecl),
+      callExpr(callee(MemsetDecl), argumentCountIs(3),
                unless(hasArgument(1, anyOf(characterLiteral(equals(
                                                static_cast<unsigned>('0'))),
                                            integerLiteral()))))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102714.346248.patch
Type: text/x-patch
Size: 2031 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210518/5ac89ac9/attachment.bin>


More information about the cfe-commits mailing list