Skip to content

Commit bf1435a

Browse files
Fix missingForLoopIncrement FPs for escaped/volatile/atomic vars
Bail out for volatile or class control variables, and when the variable's address escapes before the loop and the body has a call. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent ee9270e commit bf1435a

2 files changed

Lines changed: 91 additions & 10 deletions

File tree

‎lib/checkother.cpp‎

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5019,6 +5019,58 @@ static bool isStableLoopBound(const Token* boundTok,
50195019
return true;
50205020
}
50215021

5022+
static bool isAcceptableForLoopControlVar(const Token* tok)
5023+
{
5024+
if (!Token::Match(tok, "%var%") || tok->varId() == 0)
5025+
return false;
5026+
5027+
const Variable* var = tok->variable();
5028+
if (var && var->isVolatile())
5029+
return false;
5030+
5031+
if (!tok->valueType()) {
5032+
// Incomplete type info: allow only non-class C-like locals (not std::atomic, etc.)
5033+
return !var || !var->isClass();
5034+
}
5035+
5036+
if (tok->valueType()->volatileness != 0)
5037+
return false;
5038+
5039+
// Integral/iterator only — rejects class types such as std::atomic
5040+
return tok->valueType()->isIntegral() || tok->valueType()->type == ValueType::Type::ITERATOR;
5041+
}
5042+
5043+
static bool addressOrRefTakenBefore(const Variable* var, const Token* before)
5044+
{
5045+
if (!var || !var->nameToken() || !before)
5046+
return false;
5047+
5048+
for (const Token* tok = var->nameToken()->next(); tok && tok != before; tok = tok->next()) {
5049+
if (tok->isUnaryOp("&") && tok->astOperand1() && tok->astOperand1()->varId() == var->declarationId())
5050+
return true;
5051+
// Reference bound to the variable: T& r = var
5052+
if (Token::simpleMatch(tok, "=") && tok->astOperand2() && tok->astOperand2()->varId() == var->declarationId()) {
5053+
const Token* lhs = tok->astOperand1();
5054+
if (lhs && lhs->variable() && lhs->variable()->isReference())
5055+
return true;
5056+
}
5057+
}
5058+
return false;
5059+
}
5060+
5061+
static bool loopBodyHasCall(const Scope& scope)
5062+
{
5063+
for (const Token* tok = scope.bodyStart; tok != scope.bodyEnd; tok = tok->next()) {
5064+
if (const Token* lambdaEnd = findLambdaEndToken(tok)) {
5065+
tok = lambdaEnd;
5066+
continue;
5067+
}
5068+
if (tok->isName() && Token::simpleMatch(tok->next(), "(") && !tok->isKeyword())
5069+
return true;
5070+
}
5071+
return false;
5072+
}
5073+
50225074
static const Token* findForLoopControlVar(const Token* initTok, const Token* condTok)
50235075
{
50245076
if (!initTok || !condTok || !condTok->isComparisonOp())
@@ -5031,20 +5083,14 @@ static const Token* findForLoopControlVar(const Token* initTok, const Token* con
50315083
const Variable* var = initLhs->variable();
50325084
if (!var || !var->isLocal() || var->isStatic() || var->isReference() || var->isRValueReference())
50335085
return nullptr;
5034-
5035-
auto isAcceptableLoopVar = [](const Token* tok) -> bool {
5036-
if (!Token::Match(tok, "%var%") || tok->varId() == 0)
5037-
return false;
5038-
if (!tok->valueType())
5039-
return true; // incomplete type info; still allow for C / missing VT
5040-
return tok->valueType()->isIntegral() || tok->valueType()->type == ValueType::Type::ITERATOR;
5041-
};
5086+
if (var->isVolatile())
5087+
return nullptr;
50425088

50435089
const Token* op1 = condTok->astOperand1();
50445090
const Token* op2 = condTok->astOperand2();
5045-
if (isAcceptableLoopVar(op1) && op1->varId() == initLhs->varId())
5091+
if (isAcceptableForLoopControlVar(op1) && op1->varId() == initLhs->varId())
50465092
return op1;
5047-
if (isAcceptableLoopVar(op2) && op2->varId() == initLhs->varId())
5093+
if (isAcceptableForLoopControlVar(op2) && op2->varId() == initLhs->varId())
50485094
return op2;
50495095
return nullptr;
50505096
}
@@ -5087,6 +5133,11 @@ void CheckOtherImpl::checkMissingForLoopIncrement()
50875133
if (!isStableLoopBound(otherBound, scope.bodyStart, scope.bodyEnd, stepTok, mSettings))
50885134
continue;
50895135

5136+
// Address/reference may have escaped before the loop; a call in the body
5137+
// might update the variable through that alias.
5138+
if (addressOrRefTakenBefore(loopVarTok->variable(), forTok) && loopBodyHasCall(scope))
5139+
continue;
5140+
50905141
if (hasForLoopExit(scope, mSettings.library))
50915142
continue;
50925143

‎test/testother.cpp‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14809,6 +14809,36 @@ class TestOther : public TestFixture {
1480914809
" for (int h = 35; h > 5; k++) {}\n"
1481014810
"}\n");
1481114811
ASSERT_EQUALS("[test.cpp:3:3]: (warning) The loop variable 'h' is not updated in this for-loop, so the loop might never terminate. [missingForLoopIncrement]\n", errout_str());
14812+
14813+
// Volatile control variable whose address escapes — OK
14814+
check("void register_flag(volatile int *p);\n"
14815+
"void f(void) {\n"
14816+
" volatile int done;\n"
14817+
" register_flag(&done);\n"
14818+
" for (done = 0; done == 0;) {}\n"
14819+
"}\n", dinit(CheckOptions, $.cpp = false));
14820+
ASSERT_EQUALS("", errout_str());
14821+
14822+
// std::atomic control variable — OK
14823+
check("#include <atomic>\n"
14824+
"#include <thread>\n"
14825+
"void f() {\n"
14826+
" std::atomic<int> i;\n"
14827+
" std::thread t([&]{ i = 10; });\n"
14828+
" for (i = 0; i < 10;) {}\n"
14829+
" t.join();\n"
14830+
"}\n");
14831+
ASSERT_EQUALS("", errout_str());
14832+
14833+
// Address stored in a global pointer, then changed through a call — OK
14834+
check("int *g;\n"
14835+
"void tick(void) { (*g)++; }\n"
14836+
"void f(int n) {\n"
14837+
" int i;\n"
14838+
" g = &i;\n"
14839+
" for (i = 0; i < n;) tick();\n"
14840+
"}\n", dinit(CheckOptions, $.cpp = false));
14841+
ASSERT_EQUALS("", errout_str());
1481214842
}
1481314843
};
1481414844

0 commit comments

Comments
 (0)