Skip to content

[analyzer] Avoid crashes in the stream checker #100901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -946,14 +946,18 @@ void StreamChecker::evalFopen(const FnDescription *Desc, const CallEvent &Call,
std::tie(StateNotNull, StateNull) =
C.getConstraintManager().assumeDual(State, RetVal);

StateNotNull =
StateNotNull->set<StreamMap>(RetSym, StreamState::getOpened(Desc));
StateNull =
StateNull->set<StreamMap>(RetSym, StreamState::getOpenFailed(Desc));

C.addTransition(StateNotNull,
constructLeakNoteTag(C, RetSym, "Stream opened here"));
C.addTransition(StateNull);
if (StateNotNull)
StateNotNull =
StateNotNull->set<StreamMap>(RetSym, StreamState::getOpened(Desc));
if (StateNull)
StateNull =
StateNull->set<StreamMap>(RetSym, StreamState::getOpenFailed(Desc));

if (StateNotNull)
C.addTransition(StateNotNull,
constructLeakNoteTag(C, RetSym, "Stream opened here"));
if (StateNull)
C.addTransition(StateNull);
}

void StreamChecker::preFreopen(const FnDescription *Desc, const CallEvent &Call,
Expand Down
25 changes: 25 additions & 0 deletions clang/test/Analysis/stream-no-crash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Stream -verify %s

// This test is isolate since it uses line markers to repro the problem.
// The test is expected to find the issues noted below without crashing.

# 1 "" 1
# 1 "" 1
# 1 "" 1
# 1 "" 1 3
typedef FILE;
extern *stdout;
char a;
*fopen();
# 0 "" 2
# 7 "" 2
# 7 "" 2
# 7 "" 2
void b() {
fopen(&a, "");
int c = stdout && c;
b();
}
// expected-warning@-3{{Assigned value is garbage or undefined}}
// expected-warning@-4{{Opened stream never closed. Potential resource leak}}

Loading