Author: Haojian Wu Date: 2020-07-10T11:42:35+02:00 New Revision: 5f41ca48d1c46fc78958d47c0edfb2dbcde47217
URL: https://github.com/llvm/llvm-project/commit/5f41ca48d1c46fc78958d47c0edfb2dbcde47217 DIFF: https://github.com/llvm/llvm-project/commit/5f41ca48d1c46fc78958d47c0edfb2dbcde47217.diff LOG: [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 Added: Modified: clang-tools-extra/clang-tidy/bugprone/SuspiciousMemsetUsageCheck.cpp clang-tools-extra/test/clang-tidy/checkers/bugprone-suspicious-memset-usage.cpp Removed: ################################################################################ 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); +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits