clang-tools  11.0.0
StringConstructorCheck.cpp
Go to the documentation of this file.
1 //===--- StringConstructorCheck.cpp - clang-tidy---------------------------===//
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Tooling/FixIt.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace bugprone {
19 
20 namespace {
21 AST_MATCHER_P(IntegerLiteral, isBiggerThan, unsigned, N) {
22  return Node.getValue().getZExtValue() > N;
23 }
24 } // namespace
25 
26 StringConstructorCheck::StringConstructorCheck(StringRef Name,
27  ClangTidyContext *Context)
28  : ClangTidyCheck(Name, Context),
29  WarnOnLargeLength(Options.get("WarnOnLargeLength", true)),
30  LargeLengthThreshold(Options.get("LargeLengthThreshold", 0x800000)) {}
31 
33  Options.store(Opts, "WarnOnLargeLength", WarnOnLargeLength);
34  Options.store(Opts, "LargeLengthThreshold", LargeLengthThreshold);
35 }
36 
37 void StringConstructorCheck::registerMatchers(MatchFinder *Finder) {
38  const auto ZeroExpr = expr(ignoringParenImpCasts(integerLiteral(equals(0))));
39  const auto CharExpr = expr(ignoringParenImpCasts(characterLiteral()));
40  const auto NegativeExpr = expr(ignoringParenImpCasts(
41  unaryOperator(hasOperatorName("-"),
42  hasUnaryOperand(integerLiteral(unless(equals(0)))))));
43  const auto LargeLengthExpr = expr(ignoringParenImpCasts(
44  integerLiteral(isBiggerThan(LargeLengthThreshold))));
45  const auto CharPtrType = type(anyOf(pointerType(), arrayType()));
46 
47  // Match a string-literal; even through a declaration with initializer.
48  const auto BoundStringLiteral = stringLiteral().bind("str");
49  const auto ConstStrLiteralDecl = varDecl(
50  isDefinition(), hasType(constantArrayType()), hasType(isConstQualified()),
51  hasInitializer(ignoringParenImpCasts(BoundStringLiteral)));
52  const auto ConstPtrStrLiteralDecl = varDecl(
53  isDefinition(),
54  hasType(pointerType(pointee(isAnyCharacter(), isConstQualified()))),
55  hasInitializer(ignoringParenImpCasts(BoundStringLiteral)));
56  const auto ConstStrLiteral = expr(ignoringParenImpCasts(anyOf(
57  BoundStringLiteral, declRefExpr(hasDeclaration(anyOf(
58  ConstPtrStrLiteralDecl, ConstStrLiteralDecl))))));
59 
60  // Check the fill constructor. Fills the string with n consecutive copies of
61  // character c. [i.e string(size_t n, char c);].
62  Finder->addMatcher(
63  cxxConstructExpr(
64  hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
65  hasArgument(0, hasType(qualType(isInteger()))),
66  hasArgument(1, hasType(qualType(isInteger()))),
67  anyOf(
68  // Detect the expression: string('x', 40);
69  hasArgument(0, CharExpr.bind("swapped-parameter")),
70  // Detect the expression: string(0, ...);
71  hasArgument(0, ZeroExpr.bind("empty-string")),
72  // Detect the expression: string(-4, ...);
73  hasArgument(0, NegativeExpr.bind("negative-length")),
74  // Detect the expression: string(0x1234567, ...);
75  hasArgument(0, LargeLengthExpr.bind("large-length"))))
76  .bind("constructor"),
77  this);
78 
79  // Check the literal string constructor with char pointer and length
80  // parameters. [i.e. string (const char* s, size_t n);]
81  Finder->addMatcher(
82  cxxConstructExpr(
83  hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
84  hasArgument(0, hasType(CharPtrType)),
85  hasArgument(1, hasType(isInteger())),
86  anyOf(
87  // Detect the expression: string("...", 0);
88  hasArgument(1, ZeroExpr.bind("empty-string")),
89  // Detect the expression: string("...", -4);
90  hasArgument(1, NegativeExpr.bind("negative-length")),
91  // Detect the expression: string("lit", 0x1234567);
92  hasArgument(1, LargeLengthExpr.bind("large-length")),
93  // Detect the expression: string("lit", 5)
94  allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")),
95  hasArgument(1, ignoringParenImpCasts(
96  integerLiteral().bind("int"))))))
97  .bind("constructor"),
98  this);
99 
100  // Check the literal string constructor with char pointer.
101  // [i.e. string (const char* s);]
102  Finder->addMatcher(
103  traverse(TK_AsIs,
104  cxxConstructExpr(hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
105  hasArgument(0, expr().bind("from-ptr")),
106  hasArgument(1, unless(hasType(isInteger()))))
107  .bind("constructor")),
108  this);
109 }
110 
111 void StringConstructorCheck::check(const MatchFinder::MatchResult &Result) {
112  const ASTContext &Ctx = *Result.Context;
113  const auto *E = Result.Nodes.getNodeAs<CXXConstructExpr>("constructor");
114  assert(E && "missing constructor expression");
115  SourceLocation Loc = E->getBeginLoc();
116 
117  if (Result.Nodes.getNodeAs<Expr>("swapped-parameter")) {
118  const Expr *P0 = E->getArg(0);
119  const Expr *P1 = E->getArg(1);
120  diag(Loc, "string constructor parameters are probably swapped;"
121  " expecting string(count, character)")
122  << tooling::fixit::createReplacement(*P0, *P1, Ctx)
123  << tooling::fixit::createReplacement(*P1, *P0, Ctx);
124  } else if (Result.Nodes.getNodeAs<Expr>("empty-string")) {
125  diag(Loc, "constructor creating an empty string");
126  } else if (Result.Nodes.getNodeAs<Expr>("negative-length")) {
127  diag(Loc, "negative value used as length parameter");
128  } else if (Result.Nodes.getNodeAs<Expr>("large-length")) {
129  if (WarnOnLargeLength)
130  diag(Loc, "suspicious large length parameter");
131  } else if (Result.Nodes.getNodeAs<Expr>("literal-with-length")) {
132  const auto *Str = Result.Nodes.getNodeAs<StringLiteral>("str");
133  const auto *Lit = Result.Nodes.getNodeAs<IntegerLiteral>("int");
134  if (Lit->getValue().ugt(Str->getLength())) {
135  diag(Loc, "length is bigger than string literal size");
136  }
137  } else if (const auto *Ptr = Result.Nodes.getNodeAs<Expr>("from-ptr")) {
138  Expr::EvalResult ConstPtr;
139  if (!Ptr->isInstantiationDependent() &&
140  Ptr->EvaluateAsRValue(ConstPtr, Ctx) &&
141  ((ConstPtr.Val.isInt() && ConstPtr.Val.getInt().isNullValue()) ||
142  (ConstPtr.Val.isLValue() && ConstPtr.Val.isNullPointer()))) {
143  diag(Loc, "constructing string from nullptr is undefined behaviour");
144  }
145  }
146 }
147 
148 } // namespace bugprone
149 } // namespace tidy
150 } // namespace clang
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
StringConstructorCheck.h
Ctx
Context Ctx
Definition: TUScheduler.cpp:324
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:114
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::bugprone::StringConstructorCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: StringConstructorCheck.cpp:37
clang::tidy::readability::AST_MATCHER_P
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)
Definition: ConvertMemberFunctionsToStatic.cpp:53
clang::tidy::bugprone::StringConstructorCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: StringConstructorCheck.cpp:32
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:471
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:55
clang::tidy::bugprone::StringConstructorCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: StringConstructorCheck.cpp:111
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyCheck::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidyCheck.cpp:152
Loc
SourceLocation Loc
'#' location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:111