2017-11-23 20:33:12 +08:00
|
|
|
//===--- MisplacedOperatorInStrlenInAllocCheck.cpp - clang-tidy------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-11-23 20:33:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MisplacedOperatorInStrlenInAllocCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
2020-06-29 23:05:51 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2017-11-23 20:33:12 +08:00
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace bugprone {
|
|
|
|
|
|
|
|
void MisplacedOperatorInStrlenInAllocCheck::registerMatchers(
|
|
|
|
MatchFinder *Finder) {
|
2020-07-07 21:30:52 +08:00
|
|
|
const auto StrLenFunc = functionDecl(hasAnyName(
|
|
|
|
"::strlen", "::std::strlen", "::strnlen", "::std::strnlen", "::strnlen_s",
|
|
|
|
"::std::strnlen_s", "::wcslen", "::std::wcslen", "::wcsnlen",
|
|
|
|
"::std::wcsnlen", "::wcsnlen_s", "std::wcsnlen_s"));
|
2017-11-23 20:33:12 +08:00
|
|
|
|
|
|
|
const auto BadUse =
|
|
|
|
callExpr(callee(StrLenFunc),
|
|
|
|
hasAnyArgument(ignoringImpCasts(
|
2018-11-25 10:41:01 +08:00
|
|
|
binaryOperator(
|
|
|
|
hasOperatorName("+"),
|
|
|
|
hasRHS(ignoringParenImpCasts(integerLiteral(equals(1)))))
|
2017-11-23 20:33:12 +08:00
|
|
|
.bind("BinOp"))))
|
|
|
|
.bind("StrLen");
|
|
|
|
|
|
|
|
const auto BadArg = anyOf(
|
2018-11-25 10:41:01 +08:00
|
|
|
allOf(unless(binaryOperator(
|
2017-11-23 20:33:12 +08:00
|
|
|
hasOperatorName("+"), hasLHS(BadUse),
|
2018-11-25 10:41:01 +08:00
|
|
|
hasRHS(ignoringParenImpCasts(integerLiteral(equals(1)))))),
|
|
|
|
hasDescendant(BadUse)),
|
2017-11-23 20:33:12 +08:00
|
|
|
BadUse);
|
|
|
|
|
2020-07-07 21:30:52 +08:00
|
|
|
const auto Alloc0Func = functionDecl(
|
|
|
|
hasAnyName("::malloc", "std::malloc", "::alloca", "std::alloca"));
|
|
|
|
const auto Alloc1Func = functionDecl(
|
|
|
|
hasAnyName("::calloc", "std::calloc", "::realloc", "std::realloc"));
|
2017-11-23 20:33:12 +08:00
|
|
|
|
2017-11-23 21:12:25 +08:00
|
|
|
const auto Alloc0FuncPtr =
|
|
|
|
varDecl(hasType(isConstQualified()),
|
|
|
|
hasInitializer(ignoringParenImpCasts(
|
|
|
|
declRefExpr(hasDeclaration(Alloc0Func)))));
|
|
|
|
const auto Alloc1FuncPtr =
|
|
|
|
varDecl(hasType(isConstQualified()),
|
|
|
|
hasInitializer(ignoringParenImpCasts(
|
|
|
|
declRefExpr(hasDeclaration(Alloc1Func)))));
|
|
|
|
|
2017-11-23 20:56:23 +08:00
|
|
|
Finder->addMatcher(
|
2020-12-11 07:52:35 +08:00
|
|
|
traverse(TK_AsIs, callExpr(callee(decl(anyOf(Alloc0Func, Alloc0FuncPtr))),
|
|
|
|
hasArgument(0, BadArg))
|
|
|
|
.bind("Alloc")),
|
2019-11-12 23:15:56 +08:00
|
|
|
this);
|
|
|
|
Finder->addMatcher(
|
2020-12-11 07:52:35 +08:00
|
|
|
traverse(TK_AsIs, callExpr(callee(decl(anyOf(Alloc1Func, Alloc1FuncPtr))),
|
|
|
|
hasArgument(1, BadArg))
|
|
|
|
.bind("Alloc")),
|
2019-11-12 23:15:56 +08:00
|
|
|
this);
|
|
|
|
Finder->addMatcher(
|
2020-12-11 07:52:35 +08:00
|
|
|
traverse(TK_AsIs,
|
2019-11-12 23:15:56 +08:00
|
|
|
cxxNewExpr(isArray(), hasArraySize(BadArg)).bind("Alloc")),
|
|
|
|
this);
|
2017-11-23 20:33:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MisplacedOperatorInStrlenInAllocCheck::check(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
2017-11-23 20:56:23 +08:00
|
|
|
const Expr *Alloc = Result.Nodes.getNodeAs<CallExpr>("Alloc");
|
|
|
|
if (!Alloc)
|
|
|
|
Alloc = Result.Nodes.getNodeAs<CXXNewExpr>("Alloc");
|
2020-04-05 14:28:11 +08:00
|
|
|
assert(Alloc && "Matched node bound by 'Alloc' should be either 'CallExpr'"
|
2017-11-23 20:56:23 +08:00
|
|
|
" or 'CXXNewExpr'");
|
|
|
|
|
2017-11-23 20:33:12 +08:00
|
|
|
const auto *StrLen = Result.Nodes.getNodeAs<CallExpr>("StrLen");
|
|
|
|
const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>("BinOp");
|
|
|
|
|
|
|
|
const StringRef StrLenText = Lexer::getSourceText(
|
|
|
|
CharSourceRange::getTokenRange(StrLen->getSourceRange()),
|
|
|
|
*Result.SourceManager, getLangOpts());
|
|
|
|
const StringRef Arg0Text = Lexer::getSourceText(
|
|
|
|
CharSourceRange::getTokenRange(StrLen->getArg(0)->getSourceRange()),
|
|
|
|
*Result.SourceManager, getLangOpts());
|
2017-11-23 20:56:23 +08:00
|
|
|
const StringRef StrLenBegin = StrLenText.substr(0, StrLenText.find(Arg0Text));
|
2017-11-23 20:33:12 +08:00
|
|
|
const StringRef StrLenEnd = StrLenText.substr(
|
|
|
|
StrLenText.find(Arg0Text) + Arg0Text.size(), StrLenText.size());
|
|
|
|
|
|
|
|
const StringRef LHSText = Lexer::getSourceText(
|
|
|
|
CharSourceRange::getTokenRange(BinOp->getLHS()->getSourceRange()),
|
|
|
|
*Result.SourceManager, getLangOpts());
|
|
|
|
const StringRef RHSText = Lexer::getSourceText(
|
|
|
|
CharSourceRange::getTokenRange(BinOp->getRHS()->getSourceRange()),
|
|
|
|
*Result.SourceManager, getLangOpts());
|
|
|
|
|
|
|
|
auto Hint = FixItHint::CreateReplacement(
|
|
|
|
StrLen->getSourceRange(),
|
|
|
|
(StrLenBegin + LHSText + StrLenEnd + " + " + RHSText).str());
|
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(Alloc->getBeginLoc(),
|
2017-11-23 20:33:12 +08:00
|
|
|
"addition operator is applied to the argument of %0 instead of its "
|
2018-08-10 06:42:26 +08:00
|
|
|
"result")
|
|
|
|
<< StrLen->getDirectCallee()->getName() << Hint;
|
2017-11-23 20:33:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace bugprone
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|