10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
20 void MisplacedOperatorInStrlenInAllocCheck::registerMatchers(
21 MatchFinder *Finder) {
22 const auto StrLenFunc = functionDecl(hasAnyName(
23 "::strlen",
"::std::strlen",
"::strnlen",
"::std::strnlen",
"::strnlen_s",
24 "::std::strnlen_s",
"::wcslen",
"::std::wcslen",
"::wcsnlen",
25 "::std::wcsnlen",
"::wcsnlen_s",
"std::wcsnlen_s"));
28 callExpr(callee(StrLenFunc),
29 hasAnyArgument(ignoringImpCasts(
32 hasRHS(ignoringParenImpCasts(integerLiteral(equals(1)))))
36 const auto BadArg = anyOf(
37 allOf(unless(binaryOperator(
38 hasOperatorName(
"+"), hasLHS(BadUse),
39 hasRHS(ignoringParenImpCasts(integerLiteral(equals(1)))))),
40 hasDescendant(BadUse)),
43 const auto Alloc0Func = functionDecl(
44 hasAnyName(
"::malloc",
"std::malloc",
"::alloca",
"std::alloca"));
45 const auto Alloc1Func = functionDecl(
46 hasAnyName(
"::calloc",
"std::calloc",
"::realloc",
"std::realloc"));
48 const auto Alloc0FuncPtr =
49 varDecl(hasType(isConstQualified()),
50 hasInitializer(ignoringParenImpCasts(
51 declRefExpr(hasDeclaration(Alloc0Func)))));
52 const auto Alloc1FuncPtr =
53 varDecl(hasType(isConstQualified()),
54 hasInitializer(ignoringParenImpCasts(
55 declRefExpr(hasDeclaration(Alloc1Func)))));
58 traverse(ast_type_traits::TK_AsIs,
59 callExpr(callee(decl(anyOf(Alloc0Func, Alloc0FuncPtr))),
60 hasArgument(0, BadArg))
64 traverse(ast_type_traits::TK_AsIs,
65 callExpr(callee(decl(anyOf(Alloc1Func, Alloc1FuncPtr))),
66 hasArgument(1, BadArg))
70 traverse(ast_type_traits::TK_AsIs,
71 cxxNewExpr(isArray(), hasArraySize(BadArg)).bind(
"Alloc")),
75 void MisplacedOperatorInStrlenInAllocCheck::check(
76 const MatchFinder::MatchResult &Result) {
77 const Expr *Alloc = Result.Nodes.getNodeAs<CallExpr>(
"Alloc");
79 Alloc = Result.Nodes.getNodeAs<CXXNewExpr>(
"Alloc");
80 assert(Alloc &&
"Matched node bound by 'Alloc' should be either 'CallExpr'"
83 const auto *StrLen = Result.Nodes.getNodeAs<CallExpr>(
"StrLen");
84 const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>(
"BinOp");
86 const StringRef StrLenText = Lexer::getSourceText(
87 CharSourceRange::getTokenRange(StrLen->getSourceRange()),
88 *Result.SourceManager, getLangOpts());
89 const StringRef Arg0Text = Lexer::getSourceText(
90 CharSourceRange::getTokenRange(StrLen->getArg(0)->getSourceRange()),
91 *Result.SourceManager, getLangOpts());
92 const StringRef StrLenBegin = StrLenText.substr(0, StrLenText.find(Arg0Text));
93 const StringRef StrLenEnd = StrLenText.substr(
94 StrLenText.find(Arg0Text) + Arg0Text.size(), StrLenText.size());
96 const StringRef LHSText = Lexer::getSourceText(
97 CharSourceRange::getTokenRange(BinOp->getLHS()->getSourceRange()),
98 *Result.SourceManager, getLangOpts());
99 const StringRef RHSText = Lexer::getSourceText(
100 CharSourceRange::getTokenRange(BinOp->getRHS()->getSourceRange()),
101 *Result.SourceManager, getLangOpts());
103 auto Hint = FixItHint::CreateReplacement(
104 StrLen->getSourceRange(),
105 (StrLenBegin + LHSText + StrLenEnd +
" + " + RHSText).str());
107 diag(Alloc->getBeginLoc(),
108 "addition operator is applied to the argument of %0 instead of its "
110 << StrLen->getDirectCallee()->getName() << Hint;