Evaluate casts in programmemory (#3359)

This commit is contained in:
Paul Fultz II 2021-07-26 15:22:23 -05:00 committed by GitHub
parent 14802b932e
commit 0d3afbb954
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -725,6 +725,12 @@ void execute(const Token* expr,
*result = 0; *result = 0;
else else
*error = true; *error = true;
}
else if (expr->str() == "(" && expr->isCast()) {
if (Token::simpleMatch(expr->previous(), ">") && expr->previous()->link())
execute(expr->astOperand2(), programMemory, result, error);
else
execute(expr->astOperand1(), programMemory, result, error);
} else } else
*error = true; *error = true;
} }

View File

@ -563,6 +563,32 @@ private:
" return false;\n" " return false;\n"
"}\n"); "}\n");
ASSERT_EQUALS("test.cpp:8:style:Consider using std::any_of algorithm instead of a raw loop.\n", errout.str()); ASSERT_EQUALS("test.cpp:8:style:Consider using std::any_of algorithm instead of a raw loop.\n", errout.str());
checkNormal("bool g();\n"
"int f(int x) {\n"
" std::vector<int> v;\n"
" if (g())\n"
" v.emplace_back(x);\n"
" const auto n = (int)v.size();\n"
" for (int i = 0; i < n; ++i)\n"
" if (v[i] > 0)\n"
" return i;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkNormal("bool g();\n"
"int f(int x) {\n"
" std::vector<int> v;\n"
" if (g())\n"
" v.emplace_back(x);\n"
" const auto n = static_cast<int>(v.size());\n"
" for (int i = 0; i < n; ++i)\n"
" if (v[i] > 0)\n"
" return i;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void outOfBoundsIndexExpression() { void outOfBoundsIndexExpression() {