Skip to content

Commit

Permalink
Fix #13669 FP knownConditionTrueFalse for size variable declared in f…
Browse files Browse the repository at this point in the history
…or loop (danmar#7351)
  • Loading branch information
chrchr-github authored Mar 5, 2025
1 parent 91a7fd5 commit 6fc4d36
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2986,7 +2986,7 @@ bool isVariablesChanged(const Token* start,
const bool globalvar = std::any_of(vars.cbegin(), vars.cend(), [](const Variable* var) {
return var->isGlobal();
});
for (const Token* tok = start; tok != end; tok = tok->next()) {
for (const Token* tok = start; tok && tok != end; tok = tok->next()) {
if (tok->varId() == 0 || varids.count(tok->varId()) == 0) {
if (globalvar && Token::Match(tok, "%name% ("))
// TODO: Is global variable really changed by function call?
Expand Down
4 changes: 3 additions & 1 deletion lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2296,7 +2296,9 @@ bool Variable::isUnsigned() const
const Token * Variable::declEndToken() const
{
Token const * declEnd = typeStartToken();
while (declEnd && !Token::Match(declEnd, "[;,)={]")) {
if (declEnd->tokAt(-1) && Token::simpleMatch(declEnd->tokAt(-2), "for ("))
declEnd = nameToken();
while (declEnd && !Token::Match(declEnd, "[;:,)={]")) {
if (declEnd->link() && Token::Match(declEnd,"(|[|<"))
declEnd = declEnd->link();
declEnd = declEnd->next();
Expand Down
2 changes: 2 additions & 0 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2243,6 +2243,8 @@ struct LifetimeStore {
if (!var)
continue;
const Token * const varDeclEndToken = var->declEndToken();
if (!varDeclEndToken)
continue;
for (const Token *tok3 = tok; tok3 && tok3 != varDeclEndToken; tok3 = tok3->previous()) {
if (tok3->varId() == var->declarationId()) {
update |= LifetimeStore{tok3, message, type, inconclusive}
Expand Down
11 changes: 11 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7820,6 +7820,17 @@ class TestOther : public TestFixture {
" for (; t && t->str() == s; t = t->next());\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("void f(std::string &out, const std::vector<std::string> &list) {\n" // #13669
" for (int i = 0, size = list.size(); i < size; i++) {\n"
" out += list[i];\n"
" if (size > 0 && i < (size - 2))\n"
" out += \",\";\n"
" else if (i == (size - 1))\n"
" out += \".\";\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void duplicateExpressionTernary() { // #6391
Expand Down

0 comments on commit 6fc4d36

Please sign in to comment.