11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/ASTMatchers/ASTMatchers.h" 14 #include "clang/Lex/Lexer.h" 15 #include "clang/Tooling/FixIt.h" 23 void SuspiciousMemsetUsageCheck::registerMatchers(MatchFinder *Finder) {
28 callee(functionDecl(hasName(
"::memset"))),
29 hasArgument(1, characterLiteral(equals(static_cast<unsigned>(
'0')))
30 .bind(
"char-zero-fill")),
32 eachOf(hasArgument(0, anyOf(hasType(pointsTo(isAnyCharacter())),
33 hasType(arrayType(hasElementType(
34 isAnyCharacter()))))),
35 isInTemplateInstantiation()))),
40 Finder->addMatcher(callExpr(callee(functionDecl(hasName(
"::memset"))),
41 hasArgument(1, integerLiteral().bind(
"num-fill")),
42 unless(isInTemplateInstantiation())),
47 callExpr(callee(functionDecl(hasName(
"::memset"))),
48 unless(hasArgument(1, anyOf(characterLiteral(equals(
49 static_cast<unsigned>(
'0'))),
51 unless(isInTemplateInstantiation()))
56 void SuspiciousMemsetUsageCheck::check(
const MatchFinder::MatchResult &
Result) {
57 if (
const auto *CharZeroFill =
58 Result.Nodes.getNodeAs<CharacterLiteral>(
"char-zero-fill")) {
62 SourceRange CharRange = CharZeroFill->getSourceRange();
64 diag(CharZeroFill->getBeginLoc(),
"memset fill value is char '0', " 65 "potentially mistaken for int 0");
68 if (CharRange.getBegin().isMacroID())
70 Diag << FixItHint::CreateReplacement(
71 CharSourceRange::getTokenRange(CharRange),
"0");
74 else if (
const auto *NumFill =
75 Result.Nodes.getNodeAs<IntegerLiteral>(
"num-fill")) {
79 const auto UCharMax = (1 << Result.Context->getCharWidth()) - 1;
80 Expr::EvalResult EVResult;
81 if (!NumFill->EvaluateAsInt(EVResult, *Result.Context))
84 llvm::APSInt NumValue = EVResult.Val.getInt();
85 if (NumValue >= 0 && NumValue <= UCharMax)
88 diag(NumFill->getBeginLoc(),
"memset fill value is out of unsigned " 89 "character range, gets truncated");
92 else if (
const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"call")) {
96 const Expr *FillChar = Call->getArg(1);
97 const Expr *ByteCount = Call->getArg(2);
100 Expr::EvalResult Value2;
101 if (ByteCount->isValueDependent() ||
102 !ByteCount->EvaluateAsInt(Value2, *Result.Context) ||
103 Value2.Val.getInt() != 0)
109 Expr::EvalResult EVResult;
110 if (!FillChar->isValueDependent() &&
111 FillChar->EvaluateAsInt(EVResult, *Result.Context)) {
112 llvm::APSInt Value1 = EVResult.Val.getInt();
113 if (Value1 == 0 || Value1.isNegative())
120 auto D = diag(Call->getBeginLoc(),
121 "memset of size zero, potentially swapped arguments");
122 StringRef RHSString = tooling::fixit::getText(*ByteCount, *Result.Context);
123 StringRef LHSString = tooling::fixit::getText(*FillChar, *Result.Context);
124 if (LHSString.empty() || RHSString.empty())
127 D << tooling::fixit::createReplacement(*FillChar, RHSString)
128 << tooling::fixit::createReplacement(*ByteCount, LHSString);
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//