summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaojian Wu <hokein.wu@gmail.com>2020-07-10 11:42:04 +0200
committerHaojian Wu <hokein.wu@gmail.com>2020-07-10 11:42:35 +0200
commit5f41ca48d1c46fc78958d47c0edfb2dbcde47217 (patch)
tree370075243e86b594900f78f5026ec897b8ab2163
parent264ab1e2c815728ede5d1fce257abbd04044cc27 (diff)
downloadllvm-5f41ca48d1c46fc78958d47c0edfb2dbcde47217.tar.gz
[clang-tidy] More strict on matching the standard memset function in memset-usage check.
The check assumed the matched function call has 3 arguments, but the matcher didn't guaranteed that. Differential Revision: https://reviews.llvm.org/D83301
-rw-r--r--clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp16
-rw-r--r--clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.cpp5
2 files changed, 17 insertions, 4 deletions
diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp
index 9f98316984ed..37748d9fa8cc 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp
@@ -20,11 +20,19 @@ namespace tidy {
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 @@ 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(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()))),
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.cpp
index f33ae5ae10a8..9a7e423f4012 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.cpp
@@ -75,3 +75,8 @@ void foo(int xsize, int ysize) {
// despite v == 0.
memset(p, -1, v);
}
+
+void *memset(int);
+void NoCrash() {
+ memset(1);
+}