clang 22.0.0git
PutenvStackArrayChecker.cpp
Go to the documentation of this file.
1//== PutenvStackArrayChecker.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 PutenvStackArrayChecker which finds calls of ``putenv``
10// function with automatic array variable as the argument.
11// https://wiki.sei.cmu.edu/confluence/x/6NYxBQ
12//
13//===----------------------------------------------------------------------===//
14
23
24using namespace clang;
25using namespace ento;
26
27namespace {
28class PutenvStackArrayChecker : public Checker<check::PostCall> {
29private:
30 BugType BT{this, "'putenv' called with stack-allocated string",
32 const CallDescription Putenv{CDM::CLibrary, {"putenv"}, 1};
33
34public:
35 void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
36};
37} // namespace
38
39void PutenvStackArrayChecker::checkPostCall(const CallEvent &Call,
40 CheckerContext &C) const {
41 if (!Putenv.matches(Call))
42 return;
43
44 SVal ArgV = Call.getArgSVal(0);
45 const Expr *ArgExpr = Call.getArgExpr(0);
46
47 if (!ArgV.getAsRegion())
48 return;
49
50 const auto *SSR =
51 ArgV.getAsRegion()->getMemorySpaceAs<StackSpaceRegion>(C.getState());
52 if (!SSR)
53 return;
54 const auto *StackFrameFuncD =
55 dyn_cast_or_null<FunctionDecl>(SSR->getStackFrame()->getDecl());
56 if (StackFrameFuncD && StackFrameFuncD->isMain())
57 return;
58
59 StringRef ErrorMsg = "The 'putenv' function should not be called with "
60 "arrays that have automatic storage";
61 ExplodedNode *N = C.generateErrorNode();
62 auto Report = std::make_unique<PathSensitiveBugReport>(BT, ErrorMsg, N);
63
64 // Track the argument.
65 bugreporter::trackExpressionValue(Report->getErrorNode(), ArgExpr, *Report);
66
67 C.emitReport(std::move(Report));
68}
69
70void ento::registerPutenvStackArray(CheckerManager &Mgr) {
71 Mgr.registerChecker<PutenvStackArrayChecker>();
72}
73
74bool ento::shouldRegisterPutenvStackArray(const CheckerManager &) {
75 return true;
76}
bool matches(const CallEvent &Call) const
Returns true if the CallEvent is a call to a function that matches the CallDescription.
Represents an abstract call to a function or method along a particular path.
Definition CallEvent.h:153
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
Definition Checker.h:553
const MemSpace * getMemorySpaceAs(ProgramStateRef State) const
Definition MemRegion.h:143
const MemRegion * getAsRegion() const
Definition SVals.cpp:119
bool trackExpressionValue(const ExplodedNode *N, const Expr *E, PathSensitiveBugReport &R, TrackingOptions Opts={})
Attempts to add visitors to track expression value back to its point of origin.
The JSON file list parser is used to communicate input to InstallAPI.