Skip to content

Commit 2fac5c4

Browse files
Fix missingForLoopIncrement FPs for reference and indirect bounds
Bail whenever the loop variable's address/reference escaped before the loop, and treat pointer/reference/non-local bounds as unstable when the body has a non-const call. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent bf1435a commit 2fac5c4

2 files changed

Lines changed: 81 additions & 16 deletions

File tree

‎lib/checkother.cpp‎

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4984,6 +4984,38 @@ static bool hasForLoopExit(const Scope& scope, const Library& library)
49844984
return false;
49854985
}
49864986

4987+
static bool boundDependsOnIndirectOrNonLocal(const Token* boundTok)
4988+
{
4989+
return findAstNode(boundTok, [](const Token* tok) {
4990+
if (tok->isUnaryOp("*") || tok->str() == "[")
4991+
return true;
4992+
const Variable* var = tok->variable();
4993+
if (!var)
4994+
return false;
4995+
if (var->isReference() || var->isRValueReference())
4996+
return true;
4997+
// Global/static/member may be changed by an opaque call in the body
4998+
return !var->isLocal() && !var->isArgument();
4999+
}) != nullptr;
5000+
}
5001+
5002+
static bool rangeHasNonConstCall(const Token* start, const Token* end, const Library& library)
5003+
{
5004+
if (!start || !end)
5005+
return false;
5006+
for (const Token* tok = start; tok != end; tok = tok->next()) {
5007+
if (const Token* lambdaEnd = findLambdaEndToken(tok)) {
5008+
tok = lambdaEnd;
5009+
continue;
5010+
}
5011+
if (!tok->isName() || !Token::simpleMatch(tok->next(), "(") || tok->isKeyword())
5012+
continue;
5013+
if (!isConstFunctionCall(tok, library))
5014+
return true;
5015+
}
5016+
return false;
5017+
}
5018+
49875019
static bool isStableLoopBound(const Token* boundTok,
49885020
const Token* bodyStart,
49895021
const Token* bodyEnd,
@@ -5016,6 +5048,12 @@ static bool isStableLoopBound(const Token* boundTok,
50165048
return false;
50175049
}
50185050

5051+
// Pointer/reference/non-local bound may be changed by a non-const call in the body
5052+
// (e.g. i < *p with shrink() updating the pointee; i < lim with lim a const int&).
5053+
if (boundDependsOnIndirectOrNonLocal(boundTok) &&
5054+
rangeHasNonConstCall(bodyStart, bodyEnd, settings.library))
5055+
return false;
5056+
50195057
return true;
50205058
}
50215059

@@ -5058,19 +5096,6 @@ static bool addressOrRefTakenBefore(const Variable* var, const Token* before)
50585096
return false;
50595097
}
50605098

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-
50745099
static const Token* findForLoopControlVar(const Token* initTok, const Token* condTok)
50755100
{
50765101
if (!initTok || !condTok || !condTok->isComparisonOp())
@@ -5133,9 +5158,9 @@ void CheckOtherImpl::checkMissingForLoopIncrement()
51335158
if (!isStableLoopBound(otherBound, scope.bodyStart, scope.bodyEnd, stepTok, mSettings))
51345159
continue;
51355160

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))
5161+
// Address/reference escaped before the loop — the body (or an alias write
5162+
// like r++) may update the variable without naming it directly.
5163+
if (addressOrRefTakenBefore(loopVarTok->variable(), forTok))
51395164
continue;
51405165

51415166
if (hasForLoopExit(scope, mSettings.library))

‎test/testother.cpp‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14839,6 +14839,46 @@ class TestOther : public TestFixture {
1483914839
" for (i = 0; i < n;) tick();\n"
1484014840
"}\n", dinit(CheckOptions, $.cpp = false));
1484114841
ASSERT_EQUALS("", errout_str());
14842+
14843+
// Loop variable changed through a reference alias (no call in body) — OK
14844+
check("void f(int n) {\n"
14845+
" int i;\n"
14846+
" int &r = i;\n"
14847+
" for (i = 0; i < n;) { r++; }\n"
14848+
"}\n");
14849+
ASSERT_EQUALS("", errout_str());
14850+
14851+
check("void f(int n) {\n"
14852+
" int i = 0;\n"
14853+
" int &r = i;\n"
14854+
" for (i = 0; n > i;) { ++r; }\n"
14855+
"}\n");
14856+
ASSERT_EQUALS("", errout_str());
14857+
14858+
// Bound read through a pointer; call in body changes the pointee — OK
14859+
check("int limit = 10;\n"
14860+
"void shrink(void) { limit = 0; }\n"
14861+
"void f(void) {\n"
14862+
" const int *p = &limit;\n"
14863+
" for (int i = 0; i < *p;) { shrink(); }\n"
14864+
"}\n", dinit(CheckOptions, $.cpp = false));
14865+
ASSERT_EQUALS("", errout_str());
14866+
14867+
// Bound is a const reference parameter aliased to a global changed by a call — OK
14868+
check("int limit = 10;\n"
14869+
"void shrink() { limit = 0; }\n"
14870+
"void f(const int &lim) {\n"
14871+
" for (int i = 0; i < lim;) { shrink(); }\n"
14872+
"}\n"
14873+
"void g() { f(limit); }\n");
14874+
ASSERT_EQUALS("", errout_str());
14875+
14876+
// Bound through pointer; unknown call in body — OK
14877+
check("void g(void);\n"
14878+
"void f(const int *p) {\n"
14879+
" for (int i = 0; i < *p;) { g(); }\n"
14880+
"}\n", dinit(CheckOptions, $.cpp = false));
14881+
ASSERT_EQUALS("", errout_str());
1484214882
}
1484314883
};
1484414884

0 commit comments

Comments
 (0)