diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp --- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp +++ b/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('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('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 --- /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 +}