Fix cppcheckError with for loop (#3847)

This commit is contained in:
chrchr-github 2022-02-21 18:14:20 +01:00 committed by GitHub
parent bedde0a1df
commit 734e3ac6da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -6071,9 +6071,12 @@ static void valueFlowForLoopSimplify(Token* const bodyStart,
}
const Token* vartok = expr;
const Token* rml = nextAfterAstRightmostLeaf(expr);
const Token* rml = nextAfterAstRightmostLeaf(vartok);
if (rml)
vartok = rml->previous();
vartok = rml->str() == "]" ? rml : rml->previous();
if (vartok->str() == "]" && vartok->link()->previous())
vartok = vartok->link()->previous();
if ((tok2->str() == "&&" &&
conditionIsFalse(tok2->astOperand1(),
getProgramMemory(tok2->astTop(), expr, ValueFlow::Value(value), settings))) ||

View File

@ -4107,8 +4107,21 @@ private:
"void b(S& s) {\n"
" for (*s.a = 1;;)\n"
" if (0) {}\n"
"}\n"
"struct T { S s; };\n"
"void b(T& t) {\n"
" for (*&t.s.a[0] = 1;;)\n"
" if (0) {}\n"
"}\n";
testValueOfX(code, 0, 0); // <- don't crash
testValueOfX(code, 0, 0); // <- don't throw
code = "void f() {\n"
" int p[2];\n"
" for (p[0] = 0; p[0] <= 2; p[0]++) {\n"
" for (p[1] = 0; p[1] <= 2 - p[0]; p[1]++) {}\n"
" }\n"
"}\n";
testValueOfX(code, 0, 0); // <- don't throw
}
void valueFlowSubFunction() {