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
6 changes: 6 additions & 0 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,12 @@ static void valueFlowForwardLifetime(Token * tok, const TokenList &tokenlist, Er
} else if (tok->variable() && tok->variable()->scope()) {
const Variable *var = tok->variable();
const Token *endOfVarScope = var->scope()->bodyEnd;
// Non-local variable: do not forward past the end of the current function
if (!var->isLocal() && !var->isArgument()) {
const Token* endOfFunction = ValueFlow::getEndOfExprScope(tok);
if (endOfFunction && (!endOfVarScope || precedes(endOfFunction, endOfVarScope)))
endOfVarScope = endOfFunction;
}

std::list<ValueFlow::Value> values = tok->values();
Token *nextExpression = nextAfterAstRightmostLeaf(parent);
Expand Down
8 changes: 8 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,14 @@ class TestValueFlow : public TestFixture {
ASSERT_EQUALS(true, testLifetimeOfX(code, 5, "a ;"));
}

{
// lifetime of global array must not be forwarded into the next function
const char code[] = "int x[10];\n"
"void f() { int* p = x + 1; }\n"
"void g() { x[0] = 0; }\n";
ASSERT_EQUALS(false, testLifetimeOfX(code, 3, "x ["));
}

{
const char code[] = "void f() {\n"
" std::vector<int> v;\n"
Expand Down
Loading