Fix 10429: Regression: invalidIterator (#3603)

This commit is contained in:
Paul Fultz II 2021-12-05 08:46:52 -06:00 committed by GitHub
parent c0af66bb52
commit f64bcac004
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 4 deletions

View File

@ -50,6 +50,9 @@ R calculate(const std::string& s, const T& x, const T& y, bool* error = nullptr)
auto wrap = [](T z) {
return R{z};
};
const MathLib::bigint maxBitsShift = sizeof(MathLib::bigint) * 8;
// For portability we cannot shift signed integers by 63 bits
const MathLib::bigint maxBitsSignedShift = maxBitsShift - 1;
switch (MathLib::encodeMultiChar(s)) {
case '+':
return wrap(x + y);
@ -82,14 +85,14 @@ R calculate(const std::string& s, const T& x, const T& y, bool* error = nullptr)
case '<':
return wrap(x < y);
case '<<':
if (y >= sizeof(MathLib::bigint) * 8 || y < 0 || x < 0) {
if (y >= maxBitsSignedShift || y < 0 || x < 0) {
if (error)
*error = true;
return R{};
}
return wrap(MathLib::bigint(x) << MathLib::bigint(y));
case '>>':
if (y >= sizeof(MathLib::bigint) * 8 || y < 0 || x < 0) {
if (y >= maxBitsSignedShift || y < 0 || x < 0) {
if (error)
*error = true;
return R{};

View File

@ -145,7 +145,8 @@ struct ForwardTraversal {
// Evaluate:
// 1. RHS of assignment before LHS
// 2. Unary op before operand
if (tok->isAssignmentOp() || !secondOp)
// 3. Function arguments before function call
if (tok->isAssignmentOp() || !secondOp || isFunctionCall(tok))
std::swap(firstOp, secondOp);
if (firstOp && traverseRecursive(firstOp, f, traverseUnknown, recursion+1) == Progress::Break)
return Break();
@ -757,6 +758,21 @@ struct ForwardTraversal {
return false;
}
static bool isFunctionCall(const Token* tok)
{
if (!Token::simpleMatch(tok, "("))
return false;
if (tok->isCast())
return false;
if (!tok->isBinaryOp())
return false;
if (Token::simpleMatch(tok->link(), ") {"))
return false;
if (isUnevaluated(tok))
return false;
return Token::Match(tok->previous(), "%name%|)|]|>");
}
static Token* assignExpr(Token* tok) {
while (tok->astParent() && astIsLHS(tok)) {
if (tok->astParent()->isAssignmentOp())

View File

@ -2651,7 +2651,7 @@ struct ExpressionAnalyzer : SingleValueFlowAnalyzer {
return;
visitAstNodes(start, [&](const Token* tok) {
const bool top = depth == 0 && tok == start;
const bool ispointer = astIsPointer(tok) || astIsSmartPointer(tok);
const bool ispointer = astIsPointer(tok) || astIsSmartPointer(tok) || astIsIterator(tok);
if (!top || !ispointer || value.indirect != 0) {
for (const ValueFlow::Value& v : tok->values()) {
if (!(v.isLocalLifetimeValue() || (ispointer && v.isSymbolicValue() && v.isKnown())))

View File

@ -4173,6 +4173,15 @@ private:
" return v;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("extern bool bar(int);\n"
"void f(std::vector<int> & v) {\n"
" std::vector<int>::iterator i= v.begin();\n"
" if(i == v.end() && bar(*(i+1)) ) {}\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4] -> [test.cpp:4]: (warning) Either the condition 'i==v.end()' is redundant or there is possible dereference of an invalid iterator: i+1.\n",
errout.str());
}
void dereferenceInvalidIterator2() {