Skip to content
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

Fix 12197: FP uninitvar with two loops over buffer #6417

Merged
Merged
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
17 changes: 13 additions & 4 deletions lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ namespace {
return actions.isModified();
}

bool stopOnCondition(const Token* condTok) const
{
if (analyzer->isConditional() && findAstNode(condTok, [](const Token* tok) {
return tok->isIncompleteVar();
}))
return true;
return analyzer->stopOnCondition(condTok);
}

std::pair<bool, bool> evalCond(const Token* tok, const Token* ctx = nullptr) const {
if (!tok)
return std::make_pair(false, false);
Expand Down Expand Up @@ -194,12 +203,12 @@ namespace {
template<class T, class F, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
Progress traverseConditional(T* tok, F f, bool traverseUnknown) {
if (Token::Match(tok, "?|&&|%oror%") && tok->astOperand1() && tok->astOperand2()) {
T* condTok = tok->astOperand1();
const T* condTok = tok->astOperand1();
T* childTok = tok->astOperand2();
bool checkThen, checkElse;
std::tie(checkThen, checkElse) = evalCond(condTok);
if (!checkThen && !checkElse) {
if (!traverseUnknown && analyzer->stopOnCondition(condTok) && stopUpdates()) {
if (!traverseUnknown && stopOnCondition(condTok) && stopUpdates()) {
return Progress::Continue;
}
checkThen = true;
Expand Down Expand Up @@ -467,7 +476,7 @@ namespace {
if (updateRecursive(condTok) == Progress::Break)
return Break();
}
if (!checkThen && !checkElse && !isDoWhile && analyzer->stopOnCondition(condTok) && stopUpdates())
if (!checkThen && !checkElse && !isDoWhile && stopOnCondition(condTok) && stopUpdates())
return Break(Analyzer::Terminate::Conditional);
// condition is false, we don't enter the loop
if (checkElse)
Expand Down Expand Up @@ -689,7 +698,7 @@ namespace {
Branch elseBranch{endBlock->tokAt(2) ? endBlock->linkAt(2) : nullptr};
// Check if condition is true or false
std::tie(thenBranch.check, elseBranch.check) = evalCond(condTok);
if (!thenBranch.check && !elseBranch.check && analyzer->stopOnCondition(condTok) && stopUpdates())
if (!thenBranch.check && !elseBranch.check && stopOnCondition(condTok) && stopUpdates())
return Break(Analyzer::Terminate::Conditional);
const bool hasElse = Token::simpleMatch(endBlock, "} else {");
bool bail = false;
Expand Down
4 changes: 1 addition & 3 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,8 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
scopeList.emplace_back(this, tok, scope, Scope::eUnconditional, tok);
scope->nestedList.push_back(&scopeList.back());
scope = &scopeList.back();
} else if (scope->isExecutable()) {
endInitList.emplace(tok->link(), scope);
} else {
tok = tok->link();
endInitList.emplace(tok->link(), scope);
}
} else if (Token::Match(tok, "extern %type%")) {
const Token * ftok = tok->next();
Expand Down
14 changes: 13 additions & 1 deletion test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3645,7 +3645,7 @@ class TestUninitVar : public TestFixture {
"}");
ASSERT_EQUALS("", errout_str());

valueFlowUninit("int f(int x) {\n"
valueFlowUninit("int f(int x, int y) {\n"
" int a;\n"
" if (x)\n"
" a = y;\n"
Expand Down Expand Up @@ -6493,6 +6493,18 @@ class TestUninitVar : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (warning) Uninitialized variable: r\n", errout_str());

// #12197
valueFlowUninit("void f() {\n"
" char a[N];\n"
" for (int i = 0; i < N; i++)\n"
" a[i] = 1;\n"
" const int* p = a;\n"
" for (int i = 0; i < N; i++) {\n"
" if (p[i]) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());

// #12247
valueFlowUninit("void f() {\n"
" char a[10], *p = &a[0];\n"
Expand Down
Loading