Skip to content

Commit a7f0386

Browse files
committed
Updating PR based on review comments
1 parent d4960e1 commit a7f0386

2 files changed

Lines changed: 183 additions & 11 deletions

File tree

‎lib/checkother.cpp‎

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4910,19 +4910,45 @@ void CheckOtherImpl::overlappingWriteFunction(const Token *tok, const std::strin
49104910
// Detect for-loops that never update their control variable (#5459)
49114911
//---------------------------------------------------------------------------
49124912

4913-
static bool stepUpdatesVar(const Token* stepTok, nonneg int varId)
4913+
static void getStepTokRange(const Token* stepTok, const Token*& stepStart, const Token*& stepEnd)
49144914
{
4915-
if (!stepTok || varId == 0)
4915+
stepStart = nullptr;
4916+
stepEnd = nullptr;
4917+
if (!stepTok)
4918+
return;
4919+
stepStart = previousBeforeAstLeftmostLeaf(stepTok);
4920+
if (!stepStart)
4921+
stepStart = stepTok;
4922+
stepEnd = nextAfterAstRightmostLeaf(stepTok);
4923+
}
4924+
4925+
static bool stepMayUpdateVar(const Token* stepTok, const Token* loopVarTok, const Settings& settings)
4926+
{
4927+
if (!stepTok || !loopVarTok)
49164928
return false;
4917-
if (stepTok->isIncDecOp() && stepTok->astOperand1() && stepTok->astOperand1()->varId() == varId)
4929+
4930+
const Token* stepStart = nullptr;
4931+
const Token* stepEnd = nullptr;
4932+
getStepTokRange(stepTok, stepStart, stepEnd);
4933+
if (!stepEnd)
4934+
return true; // cannot determine step range; be conservative
4935+
4936+
// Covers ++/--/assignment and updates through calls (inc(&i), std::advance, ...)
4937+
if (findExpressionChanged(loopVarTok, stepStart, stepEnd, settings))
49184938
return true;
4919-
if (stepTok->isAssignmentOp() && stepTok->astOperand1() && stepTok->astOperand1()->varId() == varId)
4939+
4940+
// Opaque call/lambda in the step may update the variable in ways we do not see
4941+
if (findAstNode(stepTok, [](const Token* tok) {
4942+
if (findLambdaEndToken(tok))
4943+
return true;
4944+
return tok->isName() && Token::simpleMatch(tok->next(), "(") && !tok->isKeyword();
4945+
}))
49204946
return true;
4921-
// Recurse so nested updates are found, e.g. "delete *it++" or "a = b++"
4922-
return stepUpdatesVar(stepTok->astOperand1(), varId) || stepUpdatesVar(stepTok->astOperand2(), varId);
4947+
4948+
return false;
49234949
}
49244950

4925-
static bool hasForLoopExit(const Scope& scope)
4951+
static bool hasForLoopExit(const Scope& scope, const Library& library)
49264952
{
49274953
for (const Token* tok = scope.bodyStart; tok != scope.bodyEnd; tok = tok->next()) {
49284954
const Scope* escapeScope = tok->scope();
@@ -4935,7 +4961,12 @@ static bool hasForLoopExit(const Scope& scope)
49354961
tok = lambdaEnd;
49364962
continue;
49374963
}
4938-
if (!tok->isName() || !tok->isKeyword())
4964+
if (!tok->isName())
4965+
continue;
4966+
// exit/abort/longjmp/[[noreturn]] and similar
4967+
if (isEscapeFunction(tok, library))
4968+
return true;
4969+
if (!tok->isKeyword())
49394970
continue;
49404971
if (Token::Match(tok, "goto|return|throw"))
49414972
return true;
@@ -4953,13 +4984,48 @@ static bool hasForLoopExit(const Scope& scope)
49534984
return false;
49544985
}
49554986

4987+
static bool isStableLoopBound(const Token* boundTok,
4988+
const Token* bodyStart,
4989+
const Token* bodyEnd,
4990+
const Token* stepTok,
4991+
const Settings& settings)
4992+
{
4993+
if (!boundTok)
4994+
return false;
4995+
4996+
// Bound must not contain non-const calls (e.g. get()) or side effects
4997+
if (!isConstExpression(boundTok, settings.library))
4998+
return false;
4999+
5000+
// Bound must not read volatile data (e.g. i < *p with volatile int *p)
5001+
if (findAstNode(boundTok, [](const Token* tok) {
5002+
return tok->valueType() && tok->valueType()->volatileness != 0;
5003+
}))
5004+
return false;
5005+
5006+
// Bound must not be modified in the body (e.g. i < n with n-- in the body)
5007+
if (findExpressionChanged(boundTok, bodyStart, bodyEnd, settings))
5008+
return false;
5009+
5010+
// Bound must not be modified in the step (e.g. for (m = M; i < m; i++))
5011+
if (stepTok) {
5012+
const Token* stepStart = nullptr;
5013+
const Token* stepEnd = nullptr;
5014+
getStepTokRange(stepTok, stepStart, stepEnd);
5015+
if (!stepEnd || findExpressionChanged(boundTok, stepStart, stepEnd, settings))
5016+
return false;
5017+
}
5018+
5019+
return true;
5020+
}
5021+
49565022
static const Token* findForLoopControlVar(const Token* initTok, const Token* condTok)
49575023
{
49585024
if (!initTok || !condTok || !condTok->isComparisonOp())
49595025
return nullptr;
49605026

49615027
const Token* initLhs = Token::simpleMatch(initTok, "=") ? initTok->astOperand1() : nullptr;
4962-
if (!Token::Match(initLhs, "%var%") || initLhs->varId() == 0) //TODO: WILL THIS BE NULLPTR DEFERENCE IF initLhs IS SET TO NULLPTR ON LINE 4962??
5028+
if (!Token::Match(initLhs, "%var%") || initLhs->varId() == 0)
49635029
return nullptr;
49645030

49655031
const Variable* var = initLhs->variable();
@@ -5007,13 +5073,21 @@ void CheckOtherImpl::checkMissingForLoopIncrement()
50075073
continue;
50085074

50095075
const nonneg int varId = loopVarTok->varId();
5010-
if (stepUpdatesVar(stepTok, varId))
5076+
if (stepMayUpdateVar(stepTok, loopVarTok, mSettings))
50115077
continue;
50125078

50135079
if (!astHasVar(condTok, varId))
50145080
continue;
50155081

5016-
if (hasForLoopExit(scope))
5082+
// The other operand of the comparison must be a stable bound, otherwise the
5083+
// loop can terminate without updating the control variable.
5084+
const Token* otherBound = (loopVarTok == condTok->astOperand1())
5085+
? condTok->astOperand2()
5086+
: condTok->astOperand1();
5087+
if (!isStableLoopBound(otherBound, scope.bodyStart, scope.bodyEnd, stepTok, mSettings))
5088+
continue;
5089+
5090+
if (hasForLoopExit(scope, mSettings.library))
50175091
continue;
50185092

50195093
if (findExpressionChanged(loopVarTok, scope.bodyStart, scope.bodyEnd, mSettings))

‎test/testother.cpp‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14711,6 +14711,104 @@ class TestOther : public TestFixture {
1471114711
" dostuff(a);\n"
1471214712
"}\n");
1471314713
ASSERT_EQUALS("", errout_str());
14714+
14715+
// Step updates via function call — OK (false positive previously)
14716+
check("void inc(int* p) { ++*p; }\n"
14717+
"void f(int n) {\n"
14718+
" for (int i = 0; i < n; inc(&i)) {}\n"
14719+
"}\n");
14720+
ASSERT_EQUALS("", errout_str());
14721+
14722+
// Step updates via std::advance — OK
14723+
check("#include <iterator>\n"
14724+
"#include <list>\n"
14725+
"void f(std::list<int>& l) {\n"
14726+
" for (auto it = l.begin(); it != l.end(); std::advance(it, 1)) {}\n"
14727+
"}\n");
14728+
ASSERT_EQUALS("", errout_str());
14729+
14730+
// Step updates via member taking reference — OK
14731+
check("struct S { void step(int& i); };\n"
14732+
"void f(S& s, int n) {\n"
14733+
" for (int i = 0; i < n; s.step(i)) {}\n"
14734+
"}\n");
14735+
ASSERT_EQUALS("", errout_str());
14736+
14737+
// Lambda in step that updates the variable — OK
14738+
check("void f(int n) {\n"
14739+
" for (int i = 0; i < n; ([&]{ i++; })()) {}\n"
14740+
"}\n");
14741+
ASSERT_EQUALS("", errout_str());
14742+
14743+
// Other operand of condition changes in body — OK
14744+
check("void use(int);\n"
14745+
"void f(int n) {\n"
14746+
" for (int i = 0; i < n;) {\n"
14747+
" use(i);\n"
14748+
" n--;\n"
14749+
" }\n"
14750+
"}\n");
14751+
ASSERT_EQUALS("", errout_str());
14752+
14753+
// Other operand is a non-const function call — OK
14754+
check("int get();\n"
14755+
"void f() {\n"
14756+
" for (int i = 0; i != get();) {}\n"
14757+
"}\n");
14758+
ASSERT_EQUALS("", errout_str());
14759+
14760+
// Other operand reads volatile — OK
14761+
check("void f(const volatile int *p) {\n"
14762+
" for (int i = 0; i < *p;) {}\n"
14763+
"}\n");
14764+
ASSERT_EQUALS("", errout_str());
14765+
14766+
// Init assigns bound; step updates the other operand — OK
14767+
check("void f(int M) {\n"
14768+
" int i = 0;\n"
14769+
" for (int m = M; i < m; i++) {}\n"
14770+
"}\n");
14771+
ASSERT_EQUALS("", errout_str());
14772+
14773+
// noreturn/exit in body — OK
14774+
check("#include <stdlib.h>\n"
14775+
"void use(int);\n"
14776+
"void f(int n) {\n"
14777+
" for (int i = 0; i < n;) {\n"
14778+
" use(i);\n"
14779+
" exit(1);\n"
14780+
" }\n"
14781+
"}\n");
14782+
ASSERT_EQUALS("", errout_str());
14783+
14784+
// [[noreturn]] in body — OK
14785+
check("void use(int);\n"
14786+
"[[noreturn]] void fail();\n"
14787+
"void f(int n) {\n"
14788+
" for (int i = 0; i < n;) {\n"
14789+
" use(i);\n"
14790+
" fail();\n"
14791+
" }\n"
14792+
"}\n");
14793+
ASSERT_EQUALS("", errout_str());
14794+
14795+
// continue alone does not exit the loop — still warn
14796+
check("void use(int);\n"
14797+
"void f(int n) {\n"
14798+
" for (int i = 0; i < n;) {\n"
14799+
" if (n > 0)\n"
14800+
" continue;\n"
14801+
" use(i);\n"
14802+
" }\n"
14803+
"}\n");
14804+
ASSERT_EQUALS("[test.cpp:3:3]: (warning) The loop variable 'i' is not updated in this for-loop, so the loop might never terminate. [missingForLoopIncrement]\n", errout_str());
14805+
14806+
// Step updates a different variable, not the control var — still warn
14807+
check("void f() {\n"
14808+
" int k = 0;\n"
14809+
" for (int h = 35; h > 5; k++) {}\n"
14810+
"}\n");
14811+
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());
1471414812
}
1471514813
};
1471614814

0 commit comments

Comments
 (0)