157 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "MultipleNewInOneExpressionCheck.h"10#include "clang/AST/ASTContext.h"11#include "clang/ASTMatchers/ASTMatchFinder.h"12#include "clang/Lex/Lexer.h"13 14using namespace clang::ast_matchers;15 16namespace clang::tidy::bugprone {17 18// Determine if the result of an expression is "stored" in some way.19// It is true if the value is stored into a variable or used as initialization20// or passed to a function or constructor.21// For this use case compound assignments are not counted as a "store" (the 'E'22// expression should have pointer type).23static bool isExprValueStored(const Expr *E, ASTContext &C) {24 E = E->IgnoreParenCasts();25 // Get first non-paren, non-cast parent.26 ParentMapContext &PMap = C.getParentMapContext();27 DynTypedNodeList P = PMap.getParents(*E);28 if (P.size() != 1)29 return false;30 const Expr *ParentE = nullptr;31 while ((ParentE = P[0].get<Expr>()) && ParentE->IgnoreParenCasts() == E) {32 P = PMap.getParents(P[0]);33 if (P.size() != 1)34 return false;35 }36 37 if (const auto *ParentVarD = P[0].get<VarDecl>())38 return ParentVarD->getInit()->IgnoreParenCasts() == E;39 40 if (!ParentE)41 return false;42 43 if (const auto *BinOp = dyn_cast<BinaryOperator>(ParentE))44 return BinOp->getOpcode() == BO_Assign &&45 BinOp->getRHS()->IgnoreParenCasts() == E;46 47 return isa<CallExpr, CXXConstructExpr>(ParentE);48}49 50namespace {51 52AST_MATCHER_P(CXXTryStmt, hasHandlerFor,53 ast_matchers::internal::Matcher<QualType>, InnerMatcher) {54 const unsigned NH = Node.getNumHandlers();55 for (unsigned I = 0; I < NH; ++I) {56 const CXXCatchStmt *CatchS = Node.getHandler(I);57 // Check for generic catch handler (match anything).58 if (CatchS->getCaughtType().isNull())59 return true;60 ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);61 if (InnerMatcher.matches(CatchS->getCaughtType(), Finder, &Result)) {62 *Builder = std::move(Result);63 return true;64 }65 }66 return false;67}68 69AST_MATCHER(CXXNewExpr, mayThrow) {70 const FunctionDecl *OperatorNew = Node.getOperatorNew();71 if (!OperatorNew)72 return false;73 return !OperatorNew->getType()->castAs<FunctionProtoType>()->isNothrow();74}75 76} // namespace77 78void MultipleNewInOneExpressionCheck::registerMatchers(MatchFinder *Finder) {79 auto BadAllocType =80 recordType(hasDeclaration(cxxRecordDecl(hasName("::std::bad_alloc"))));81 auto ExceptionType =82 recordType(hasDeclaration(cxxRecordDecl(hasName("::std::exception"))));83 auto BadAllocReferenceType = referenceType(pointee(BadAllocType));84 auto ExceptionReferenceType = referenceType(pointee(ExceptionType));85 86 auto CatchBadAllocType =87 qualType(hasCanonicalType(anyOf(BadAllocType, BadAllocReferenceType,88 ExceptionType, ExceptionReferenceType)));89 auto BadAllocCatchingTryBlock = cxxTryStmt(hasHandlerFor(CatchBadAllocType));90 91 auto NewExprMayThrow = cxxNewExpr(mayThrow());92 auto HasNewExpr1 = expr(anyOf(NewExprMayThrow.bind("new1"),93 hasDescendant(NewExprMayThrow.bind("new1"))));94 auto HasNewExpr2 = expr(anyOf(NewExprMayThrow.bind("new2"),95 hasDescendant(NewExprMayThrow.bind("new2"))));96 97 Finder->addMatcher(98 callExpr(99 hasAnyArgument(expr(HasNewExpr1).bind("arg1")),100 hasAnyArgument(101 expr(HasNewExpr2, unless(equalsBoundNode("arg1"))).bind("arg2")),102 hasAncestor(BadAllocCatchingTryBlock)),103 this);104 Finder->addMatcher(105 cxxConstructExpr(106 hasAnyArgument(expr(HasNewExpr1).bind("arg1")),107 hasAnyArgument(108 expr(HasNewExpr2, unless(equalsBoundNode("arg1"))).bind("arg2")),109 unless(isListInitialization()),110 hasAncestor(BadAllocCatchingTryBlock)),111 this);112 Finder->addMatcher(binaryOperator(hasLHS(HasNewExpr1), hasRHS(HasNewExpr2),113 unless(hasAnyOperatorName("&&", "||", ",")),114 hasAncestor(BadAllocCatchingTryBlock)),115 this);116 Finder->addMatcher(117 cxxNewExpr(mayThrow(),118 hasDescendant(NewExprMayThrow.bind("new2_in_new1")),119 hasAncestor(BadAllocCatchingTryBlock))120 .bind("new1"),121 this);122}123 124void MultipleNewInOneExpressionCheck::check(125 const MatchFinder::MatchResult &Result) {126 const auto *NewExpr1 = Result.Nodes.getNodeAs<CXXNewExpr>("new1");127 const auto *NewExpr2 = Result.Nodes.getNodeAs<CXXNewExpr>("new2");128 const auto *NewExpr2InNewExpr1 =129 Result.Nodes.getNodeAs<CXXNewExpr>("new2_in_new1");130 if (!NewExpr2)131 NewExpr2 = NewExpr2InNewExpr1;132 assert(NewExpr1 && NewExpr2 && "Bound nodes not found.");133 134 // No warning if both allocations are not stored.135 // The value may be intentionally not stored (no deallocations needed or136 // self-destructing object).137 if (!isExprValueStored(NewExpr1, *Result.Context) &&138 !isExprValueStored(NewExpr2, *Result.Context))139 return;140 141 // In C++17 sequencing of a 'new' inside constructor arguments of another142 // 'new' is fixed. Still a leak can happen if the returned value from the143 // first 'new' is not saved (yet) and the second fails.144 if (getLangOpts().CPlusPlus17 && NewExpr2InNewExpr1)145 diag(NewExpr1->getBeginLoc(),146 "memory allocation may leak if an other allocation is sequenced after "147 "it and throws an exception")148 << NewExpr1->getSourceRange() << NewExpr2->getSourceRange();149 else150 diag(NewExpr1->getBeginLoc(),151 "memory allocation may leak if an other allocation is sequenced after "152 "it and throws an exception; order of these allocations is undefined")153 << NewExpr1->getSourceRange() << NewExpr2->getSourceRange();154}155 156} // namespace clang::tidy::bugprone157