1 //=== StringChecker.cpp -------------------------------------------*- C++ -*--//
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 //
9 // This file implements the modeling of the std::basic_string type.
10 // This involves checking preconditions of the operations and applying the
11 // effects of the operations, e.g. their post-conditions.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 
20 using namespace clang;
21 using namespace ento;
22 
23 namespace {
24 class StringChecker : public Checker<check::PreCall> {
25   BugType BT_Null{this, "Dereference of null pointer", categories::LogicError};
26   mutable const FunctionDecl *StringConstCharPtrCtor = nullptr;
27   mutable CanQualType SizeTypeTy;
28   const CallDescription TwoParamStdStringCtor = {
29       {"std", "basic_string", "basic_string"}, 2, 2};
30 
31   bool isCharToStringCtor(const CallEvent &Call, const ASTContext &ACtx) const;
32 
33 public:
34   void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
35 };
36 
37 bool StringChecker::isCharToStringCtor(const CallEvent &Call,
38                                        const ASTContext &ACtx) const {
39   if (!Call.isCalled(TwoParamStdStringCtor))
40     return false;
41   const auto *FD = dyn_cast<FunctionDecl>(Call.getDecl());
42   assert(FD);
43 
44   // See if we already cached it.
45   if (StringConstCharPtrCtor && StringConstCharPtrCtor == FD)
46     return true;
47 
48   // Verify that the parameters have the expected types:
49   // - arg 1: `const CharT *`
50   // - arg 2: some allocator - which is definately not `size_t`.
51   const QualType Arg1Ty = Call.getArgExpr(0)->getType().getCanonicalType();
52   const QualType Arg2Ty = Call.getArgExpr(1)->getType().getCanonicalType();
53 
54   if (!Arg1Ty->isPointerType())
55     return false;
56 
57   // It makes sure that we don't select the `string(const char* p, size_t len)`
58   // overload accidentally.
59   if (Arg2Ty.getCanonicalType() == ACtx.getSizeType())
60     return false;
61 
62   StringConstCharPtrCtor = FD; // Cache the decl of the right overload.
63   return true;
64 }
65 
66 void StringChecker::checkPreCall(const CallEvent &Call,
67                                  CheckerContext &C) const {
68   if (!isCharToStringCtor(Call, C.getASTContext()))
69     return;
70   const auto Param = Call.getArgSVal(0).getAs<Loc>();
71   if (!Param.hasValue())
72     return;
73 
74   // We managed to constrain the parameter to non-null.
75   ProgramStateRef NotNull, Null;
76   std::tie(NotNull, Null) = C.getState()->assume(*Param);
77 
78   if (NotNull) {
79     const auto Callback = [Param](PathSensitiveBugReport &BR) -> std::string {
80       return BR.isInteresting(*Param) ? "Assuming the pointer is not null."
81                                       : "";
82     };
83 
84     // Emit note only if this operation constrained the pointer to be null.
85     C.addTransition(NotNull, Null ? C.getNoteTag(Callback) : nullptr);
86     return;
87   }
88 
89   // We found a path on which the parameter is NULL.
90   if (ExplodedNode *N = C.generateErrorNode(C.getState())) {
91     auto R = std::make_unique<PathSensitiveBugReport>(
92         BT_Null, "The parameter must not be null", N);
93     bugreporter::trackExpressionValue(N, Call.getArgExpr(0), *R);
94     C.emitReport(std::move(R));
95   }
96 }
97 
98 } // end anonymous namespace
99 
100 void ento::registerStringChecker(CheckerManager &Mgr) {
101   Mgr.registerChecker<StringChecker>();
102 }
103 
104 bool ento::shouldRegisterStringChecker(const CheckerManager &) { return true; }
105