[clang-tools-extra] 9f36306 - [clang-tidy] Fix a crash on invalid code for memset-usage check.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Wed May 19 01:04:30 PDT 2021
Author: Haojian Wu
Date: 2021-05-19T09:53:18+02:00
New Revision: 9f36306cc9ac6d1d1019831d40865d0d54563379
URL: https://github.com/llvm/llvm-project/commit/9f36306cc9ac6d1d1019831d40865d0d54563379
DIFF: https://github.com/llvm/llvm-project/commit/9f36306cc9ac6d1d1019831d40865d0d54563379.diff
LOG: [clang-tidy] Fix a crash on invalid code for memset-usage check.
Differential Revision: https://reviews.llvm.org/D102714
Added:
clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.c
Modified:
clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp
index 341ba6ccc09fa..1abe470682329 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp
@@ -32,7 +32,7 @@ void SuspiciousMemsetUsageCheck::registerMatchers(MatchFinder *Finder) {
// 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 @@ void SuspiciousMemsetUsageCheck::registerMatchers(MatchFinder *Finder) {
// 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()))))
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.c
new file mode 100644
index 0000000000000..61e7de50953e8
--- /dev/null
+++ b/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
+}
More information about the cfe-commits
mailing list