Skip to content

Commit 644096a

Browse files
committed
Fix #5459 Detect Endless For Loops
1 parent 985cbbe commit 644096a

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

‎lib/checkother.cpp‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4914,13 +4914,12 @@ static bool stepUpdatesVar(const Token* stepTok, nonneg int varId)
49144914
{
49154915
if (!stepTok || varId == 0)
49164916
return false;
4917-
if (stepTok->str() == ",")
4918-
return stepUpdatesVar(stepTok->astOperand1(), varId) || stepUpdatesVar(stepTok->astOperand2(), varId);
49194917
if (stepTok->isIncDecOp() && stepTok->astOperand1() && stepTok->astOperand1()->varId() == varId)
49204918
return true;
4921-
if (stepTok->isAssignmentOp() && stepTok->astOperand1() && stepTok->astOperand1()->varId() == varId) // TODO: WILL THIS RETURN TRUE IF STEP IS SOMETHING LIKE: i=i
4919+
if (stepTok->isAssignmentOp() && stepTok->astOperand1() && stepTok->astOperand1()->varId() == varId)
49224920
return true;
4923-
return false;
4921+
// Recurse so nested updates are found, e.g. "delete *it++" or "a = b++"
4922+
return stepUpdatesVar(stepTok->astOperand1(), varId) || stepUpdatesVar(stepTok->astOperand2(), varId);
49244923
}
49254924

49264925
static bool hasForLoopExit(const Scope& scope)
@@ -4938,7 +4937,7 @@ static bool hasForLoopExit(const Scope& scope)
49384937
}
49394938
if (!tok->isName() || !tok->isKeyword())
49404939
continue;
4941-
if (Token::Match(tok, "goto|return|throw")) //TODO: WILL THIS RETURN TRUE IF GOTO STAYS WITHIN THE LOOP
4940+
if (Token::Match(tok, "goto|return|throw"))
49424941
return true;
49434942
if (tok->str() != "break")
49444943
continue;

‎test/testother.cpp‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6682,7 +6682,8 @@ class TestOther : public TestFixture {
66826682
" c ++;\n"
66836683
" }\n"
66846684
"}\n");
6685-
ASSERT_EQUALS("[test.cpp:2:31]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? [constStatement]\n", errout_str());
6685+
ASSERT_EQUALS("[test.cpp:2:31]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? [constStatement]\n"
6686+
"[test.cpp:2:5]: (warning) The loop variable 'i' is not updated in this for-loop, so the loop might never terminate. [missingForLoopIncrement]\n", errout_str());
66866687

66876688
check("void foo(int c) {\n"
66886689
" for (; running == 1;) {\n"
@@ -8641,7 +8642,8 @@ class TestOther : public TestFixture {
86418642
" if( i != 0 ) {}\n"
86428643
" }\n"
86438644
"}\n");
8644-
ASSERT_EQUALS("[test.cpp:2:17] -> [test.cpp:3:15]: (style) The comparison 'i != 0' is always false. [knownConditionTrueFalse]\n", errout_str());
8645+
ASSERT_EQUALS("[test.cpp:2:17] -> [test.cpp:3:15]: (style) The comparison 'i != 0' is always false. [knownConditionTrueFalse]\n"
8646+
"[test.cpp:2:5]: (warning) The loop variable 'i' is not updated in this for-loop, so the loop might never terminate. [missingForLoopIncrement]\n", errout_str());
86458647

86468648
check("void f() {\n"
86478649
" for(int i = 0; i < 10;) {\n"
@@ -14694,6 +14696,21 @@ class TestOther : public TestFixture {
1469414696
" }\n"
1469514697
"}\n");
1469614698
ASSERT_EQUALS("", errout_str());
14699+
14700+
// Nested update in step — OK
14701+
check("void f(const std::vector<int*>& v) {\n"
14702+
" for (auto it = v.begin(); it != v.end(); delete *it++);\n"
14703+
"}\n");
14704+
ASSERT_EQUALS("", errout_str());
14705+
14706+
// Loop var updated on RHS of step assignment — OK
14707+
check("void dostuff(int);\n"
14708+
"void foo(int num) {\n"
14709+
" int a = num;\n"
14710+
" for (int b = 0; b < num; a = b++)\n"
14711+
" dostuff(a);\n"
14712+
"}\n");
14713+
ASSERT_EQUALS("", errout_str());
1469714714
}
1469814715
};
1469914716

0 commit comments

Comments
 (0)