[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang / lib / StaticAnalyzer / Checkers / cert / PutenvWithAutoChecker.cpp
blobeae162cda6931073b0d965a8b006f30f59e28fe4
1 //== PutenvWithAutoChecker.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 defines PutenvWithAutoChecker which finds calls of ``putenv``
10 // function with automatic variable as the argument.
11 // https://wiki.sei.cmu.edu/confluence/x/6NYxBQ
13 //===----------------------------------------------------------------------===//
15 #include "../AllocationState.h"
16 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
17 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
25 using namespace clang;
26 using namespace ento;
28 namespace {
29 class PutenvWithAutoChecker : public Checker<check::PostCall> {
30 private:
31 BugType BT{this, "'putenv' function should not be called with auto variables",
32 categories::SecurityError};
33 const CallDescription Putenv{{"putenv"}, 1};
35 public:
36 void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
38 } // namespace
40 void PutenvWithAutoChecker::checkPostCall(const CallEvent &Call,
41 CheckerContext &C) const {
42 if (!Putenv.matches(Call))
43 return;
45 SVal ArgV = Call.getArgSVal(0);
46 const Expr *ArgExpr = Call.getArgExpr(0);
47 const MemSpaceRegion *MSR = ArgV.getAsRegion()->getMemorySpace();
49 if (!isa<StackSpaceRegion>(MSR))
50 return;
52 StringRef ErrorMsg = "The 'putenv' function should not be called with "
53 "arguments that have automatic storage";
54 ExplodedNode *N = C.generateErrorNode();
55 auto Report = std::make_unique<PathSensitiveBugReport>(BT, ErrorMsg, N);
57 // Track the argument.
58 bugreporter::trackExpressionValue(Report->getErrorNode(), ArgExpr, *Report);
60 C.emitReport(std::move(Report));
63 void ento::registerPutenvWithAuto(CheckerManager &Mgr) {
64 Mgr.registerChecker<PutenvWithAutoChecker>();
67 bool ento::shouldRegisterPutenvWithAuto(const CheckerManager &) { return true; }