Skip to content

Commit 807ce9f

Browse files
Refs #14935: Fix index values in function call in for loop (#8753)
1 parent 7474765 commit 807ce9f

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

lib/astutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2778,7 +2778,7 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings &settings,
27782778
if (ftok->str() == "(" && Token::simpleMatch(ftok->astOperand1(), "[")) // operator() on array element, bail out
27792779
return true;
27802780
const Token * ptok = tok2;
2781-
while (Token::Match(ptok->astParent(), ".|::|["))
2781+
while (Token::Match(ptok->astParent(), ".|::"))
27822782
ptok = ptok->astParent();
27832783
int pindirect = indirect;
27842784
if (indirect == 0 && astIsLHS(tok2) && Token::Match(ptok, ". %var%") && astIsPointer(ptok->next()))

test/testvalueflow.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5112,6 +5112,27 @@ class TestValueFlow : public TestFixture {
51125112
++it;
51135113
ASSERT_EQUALS(0, it->intvalue);
51145114
ASSERT(it->isPossible());
5115+
5116+
code = "void g(int*);\n"
5117+
"void f(int* a) {\n"
5118+
" for (int i = 0; i < 5; ++i) {\n"
5119+
" g(&a[i]);\n"
5120+
" }\n"
5121+
"}\n";
5122+
values = tokenValues(code, "i ]");
5123+
ASSERT_EQUALS(4, values.size());
5124+
it = values.begin();
5125+
ASSERT_EQUALS(0, it->intvalue);
5126+
ASSERT(it->isPossible());
5127+
++it;
5128+
ASSERT_EQUALS(-1, it->intvalue);
5129+
ASSERT(it->isImpossible());
5130+
++it;
5131+
ASSERT_EQUALS(4, it->intvalue);
5132+
ASSERT(it->isPossible());
5133+
++it;
5134+
ASSERT_EQUALS(5, it->intvalue);
5135+
ASSERT(it->isImpossible());
51155136
}
51165137

51175138
void valueFlowSubFunction() {

0 commit comments

Comments
 (0)