25 using namespace clang;
33 enum Kind { Opened, Closed } K;
34 StreamState(
Kind InK) : K(InK) { }
37 bool isOpened()
const {
return K == Opened; }
38 bool isClosed()
const {
return K == Closed; }
40 static StreamState getOpened() {
return StreamState(Opened); }
41 static StreamState getClosed() {
return StreamState(Closed); }
46 void Profile(llvm::FoldingSetNodeID &
ID)
const {
51 class SimpleStreamChecker :
public Checker<check::PostCall,
54 check::PointerEscape> {
57 std::unique_ptr<BugType> DoubleCloseBugType;
58 std::unique_ptr<BugType> LeakBugType;
60 void reportDoubleClose(
SymbolRef FileDescSym,
67 bool guaranteedNotToCloseFile(
const CallEvent &Call)
const;
70 SimpleStreamChecker();
99 bool VisitSymbol(
SymbolRef sym)
override {
106 SimpleStreamChecker::SimpleStreamChecker()
107 : OpenFn(
"fopen"), CloseFn(
"fclose", 1) {
109 DoubleCloseBugType.reset(
110 new BugType(
this,
"Double fclose",
"Unix Stream API Error"));
113 new BugType(
this,
"Resource Leak",
"Unix Stream API Error"));
115 LeakBugType->setSuppressOnSink(
true);
118 void SimpleStreamChecker::checkPostCall(
const CallEvent &Call,
133 State = State->set<StreamMap>(FileDesc, StreamState::getOpened());
137 void SimpleStreamChecker::checkPreCall(
const CallEvent &Call,
152 const StreamState *SS = State->get<StreamMap>(FileDesc);
153 if (SS && SS->isClosed()) {
154 reportDoubleClose(FileDesc, Call, C);
159 State = State->set<StreamMap>(FileDesc, StreamState::getClosed());
165 if (IsSymDead && SS.isOpened()) {
175 void SimpleStreamChecker::checkDeadSymbols(
SymbolReaper &SymReaper,
178 SymbolVector LeakedStreams;
179 StreamMapTy TrackedStreams = State->get<StreamMap>();
181 E = TrackedStreams.end();
I !=
E; ++
I) {
183 bool IsSymDead = SymReaper.
isDead(Sym);
186 if (
isLeaked(Sym,
I->second, IsSymDead, State))
187 LeakedStreams.push_back(Sym);
191 State = State->remove<StreamMap>(Sym);
197 reportLeaks(LeakedStreams, C, N);
200 void SimpleStreamChecker::reportDoubleClose(
SymbolRef FileDescSym,
210 auto R = llvm::make_unique<BugReport>(*DoubleCloseBugType,
211 "Closing a previously closed file stream", ErrNode);
213 R->markInteresting(FileDescSym);
222 for (
SymbolRef LeakedStream : LeakedStreams) {
223 auto R = llvm::make_unique<BugReport>(*LeakBugType,
224 "Opened file is never closed; potential resource leak", ErrNode);
225 R->markInteresting(LeakedStream);
230 bool SimpleStreamChecker::guaranteedNotToCloseFile(
const CallEvent &Call)
const{
257 for (InvalidatedSymbols::const_iterator
I = Escaped.begin(),
264 State = State->remove<StreamMap>(Sym);
virtual SVal getArgSVal(unsigned Index) const
Returns the value of a given argument at the time of the call.
ExplodedNode * generateErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
bool isInSystemHeader() const
Returns true if the callee is known to be from a system header.
bool operator==(CanQual< T > x, CanQual< U > y)
virtual bool argumentsMayEscape() const
Returns true if any of the arguments are known to escape to long- term storage, even if this method w...
ExplodedNode * addTransition(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generates a new transition in the program state graph (ExplodedGraph).
bool isCalled(const CallDescription &CD) const
Returns true if the CallEvent is a call to a function that matches the CallDescription.
ConditionTruthVal isNull(ProgramStateRef State, SymbolRef Sym)
Convenience method to query the state to see if a symbol is null or not null, or if neither assumptio...
static bool isLeaked(SymbolRef Sym, const StreamState &SS, bool IsSymDead, ProgramStateRef State)
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
detail::InMemoryDirectory::const_iterator I
bool isGlobalCFunction(StringRef SpecificName=StringRef()) const
Returns true if the callee is an externally-visible function in the top-level namespace, such as malloc.
#define REGISTER_MAP_WITH_PROGRAMSTATE(Name, Key, Value)
Declares an immutable map of type NameTy, suitable for placement into the ProgramState.
bool isDead(SymbolRef sym) const
Returns whether or not a symbol has been confirmed dead.
const ProgramStateRef & getState() const
The pointer has been passed to a function call directly.
ExplodedNode * generateNonFatalErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
CHECKER * registerChecker()
Used to register checkers.
This class represents a description of a function call using the number of arguments and the name of ...
const TemplateArgument * iterator
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx)
A class responsible for cleaning up unused symbols.
virtual SourceRange getSourceRange() const
Returns a source range for the entire call, suitable for outputting in diagnostics.
detail::InMemoryDirectory::const_iterator E
bool isConstrainedTrue() const
Return true if the constraint is perfectly constrained to 'true'.
Represents an abstract call to a function or method along a particular path.
PointerEscapeKind
Describes the different reasons a pointer escapes during analysis.
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
SVal getReturnValue() const
Returns the return value of the call.