Skip to content
Open
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
5 changes: 5 additions & 0 deletions lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ namespace {
bool structuralUnknown = false;
const bool structuralEscape = isEscapeScope(branch.endBlock, structuralUnknown);
branch.escapeUnknown = !structuralEscape || structuralUnknown;
// The traversal stopped at the escape, so the rest of the scope was not walked; a
// fall-through path could still modify the value there - include the whole scope's
// actions so isModified() sees it.
if (branch.escapeUnknown)
branch.action |= analyzeScope(branch.endBlock);
} else {
// Detect an escape the traversal did not flag (e.g. an unknown noreturn call);
// escapeUnknown reports a possible (unknown) escape.
Expand Down
32 changes: 32 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3214,6 +3214,38 @@ class TestValueFlow : public TestFixture {
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 3U, 0));

code = "bool f();\n" // a modification after a conditional escape must still be seen
"void g() {\n"
" bool x = false;\n"
" if (f()) {\n"
" if (f()) return;\n"
" if (f()) x = true;\n"
" }\n"
" if (x) {}\n"
"}\n";
ASSERT_EQUALS(true, testValueOfX(code, 8U, 0));
ASSERT_EQUALS(false, testValueOfXKnown(code, 8U, 0));

code = "bool f();\n"
"void g() {\n"
" bool x = false;\n"
" if (f()) {\n"
" if (f()) return;\n"
" x = true;\n"
" }\n"
" if (x) {}\n"
"}\n";
ASSERT_EQUALS(true, testValueOfX(code, 8U, 0));
ASSERT_EQUALS(false, testValueOfXKnown(code, 8U, 0));

code = "bool f();\n" // the branch always escapes - keep the known value
"void g() {\n"
" bool x = false;\n"
" if (f()) { x = true; return; }\n"
" if (x) {}\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 5U, 0));
}

void valueFlowAfterSwap()
Expand Down
Loading