clang 19.0.0git
PutenvWithAutoChecker.cpp
Go to the documentation of this file.
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
12//
13//===----------------------------------------------------------------------===//
14
15#include "../AllocationState.h"
24
25using namespace clang;
26using namespace ento;
27
28namespace {
29class PutenvWithAutoChecker : public Checker<check::PostCall> {
30private:
31 BugType BT{this, "'putenv' function should not be called with auto variables",
33 const CallDescription Putenv{CDM::CLibrary, {"putenv"}, 1};
34
35public:
36 void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
37};
38} // namespace
39
40void PutenvWithAutoChecker::checkPostCall(const CallEvent &Call,
41 CheckerContext &C) const {
42 if (!Putenv.matches(Call))
43 return;
44
45 SVal ArgV = Call.getArgSVal(0);
46 const Expr *ArgExpr = Call.getArgExpr(0);
47 const MemSpaceRegion *MSR = ArgV.getAsRegion()->getMemorySpace();
48
49 if (!isa<StackSpaceRegion>(MSR))
50 return;
51
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);
56
57 // Track the argument.
58 bugreporter::trackExpressionValue(Report->getErrorNode(), ArgExpr, *Report);
59
60 C.emitReport(std::move(Report));
61}
62
63void ento::registerPutenvWithAuto(CheckerManager &Mgr) {
64 Mgr.registerChecker<PutenvWithAutoChecker>();
65}
66
67bool ento::shouldRegisterPutenvWithAuto(const CheckerManager &) { return true; }
This represents one expression.
Definition: Expr.h:110
A CallDescription is a pattern that can be used to match calls based on the qualified name and the ar...
Represents an abstract call to a function or method along a particular path.
Definition: CallEvent.h:153
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemSpaceRegion * getMemorySpace() const
Definition: MemRegion.cpp:1317
MemSpaceRegion - A memory region that represents a "memory space"; for example, the set of global var...
Definition: MemRegion.h:203
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:55
const MemRegion * getAsRegion() const
Definition: SVals.cpp:120
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.
const char *const SecurityError
The JSON file list parser is used to communicate input to InstallAPI.