Fix 10570: Improve check; condition then pointer dereference, different pointers (#4216)

* Try to use after assign in loop

* Update valueflow forward to handle init tokens

* Fix tests

* Make test TODO

* Format

* Add tests

* Format

* Fix ubsan error

* Use simpleMatch
This commit is contained in:
Paul Fultz II 2022-06-16 12:26:23 -05:00 committed by GitHub
parent de51ebbcf4
commit 9cecc8468e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 10 deletions

View File

@ -575,7 +575,23 @@ struct ForwardTraversal {
tok = nextAfterAstRightmostLeaf(assignTok);
if (!tok)
return Break();
} else if (tok->str() == "break") {
} else if (Token::simpleMatch(tok, ") {") && Token::Match(tok->link()->previous(), "for|while (")) {
// In the middle of a loop structure so bail
return Break(Analyzer::Terminate::Bail);
} else if (tok->str() == ";" && tok->astParent()) {
Token* top = tok->astTop();
if (top && Token::Match(top->previous(), "for|while (") && Token::simpleMatch(top->link(), ") {")) {
Token* endCond = top->link();
Token* endBlock = endCond->linkAt(1);
Token* condTok = getCondTok(top);
Token* stepTok = getStepTok(top);
// The semicolon should belong to the initTok otherwise something went wrong, so just bail
if (tok->astOperand2() != condTok && !Token::simpleMatch(tok->astOperand2(), ";"))
return Break(Analyzer::Terminate::Bail);
if (updateLoop(end, endBlock, condTok, nullptr, stepTok) == Progress::Break)
return Break();
}
} else if (tok->str() == "break") {
const Token *scopeEndToken = findNextTokenFromBreak(tok);
if (!scopeEndToken)
return Break();
@ -640,7 +656,8 @@ struct ForwardTraversal {
} else if (Token::simpleMatch(tok->next(), "else {")) {
tok = tok->linkAt(2);
}
} else if (tok->isControlFlowKeyword() && Token::Match(tok, "if|while|for (") && Token::simpleMatch(tok->next()->link(), ") {")) {
} else if (tok->isControlFlowKeyword() && Token::Match(tok, "if|while|for (") &&
Token::simpleMatch(tok->next()->link(), ") {")) {
Token* endCond = tok->next()->link();
Token* endBlock = endCond->next()->link();
Token* condTok = getCondTok(tok);

View File

@ -105,6 +105,7 @@
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <exception>
@ -918,9 +919,9 @@ static void setTokenValue(Token* tok,
const double floatValue1 = value1.isFloatValue() ? value1.floatValue : value1.intvalue;
const double floatValue2 = value2.isFloatValue() ? value2.floatValue : value2.intvalue;
const MathLib::bigint intValue1 =
value1.isFloatValue() ? static_cast<MathLib::bigint>(value1.floatValue) : value1.intvalue;
value1.isFloatValue() ? std::llround(value1.floatValue) : value1.intvalue;
const MathLib::bigint intValue2 =
value2.isFloatValue() ? static_cast<MathLib::bigint>(value2.floatValue) : value2.intvalue;
value2.isFloatValue() ? std::llround(value2.floatValue) : value2.intvalue;
if ((value1.isFloatValue() || value2.isFloatValue()) && Token::Match(parent, "&|^|%|<<|>>|==|!=|%or%"))
continue;
if (Token::Match(parent, "==|!=")) {
@ -5444,7 +5445,10 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat
std::unordered_map<nonneg int, std::unordered_set<nonneg int>> backAssigns;
for (Token* tok = const_cast<Token*>(scope->bodyStart); tok != scope->bodyEnd; tok = tok->next()) {
// Assignment
if ((tok->str() != "=" && !isVariableInit(tok)) || (tok->astParent()))
if (tok->str() != "=" && !isVariableInit(tok))
continue;
if (tok->astParent() && !(tok->astParent()->str() == ";" && astIsLHS(tok)))
continue;
// Lhs should be a variable

View File

@ -2380,7 +2380,8 @@ private:
" some_condition ? 0 : a[i-1];\n"
" }\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Array index -1 is out of bounds.\n", "", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Array 'a[10]' accessed at index -1, which is out of bounds.\n",
errout.str());
check("void f() {\n"
" int a[10];\n"

View File

@ -2518,13 +2518,16 @@ private:
" {\n"
" if (a) {\n"
" if (b)\n"
" foo.erase(it);\n"
" foo.erase(it);\n" // <- TODO: erase shound't cause inconclusive valueflow
" else\n"
" *it = 0;\n"
" }\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:7] -> [test.cpp:8] -> [test.cpp:8] -> [test.cpp:7] -> [test.cpp:5] -> [test.cpp:9] -> [test.cpp:3] -> [test.cpp:5]: (error) Using iterator to local container 'foo' that may be invalid.\n", errout.str());
TODO_ASSERT_EQUALS(
"[test.cpp:5] -> [test.cpp:7] -> [test.cpp:8] -> [test.cpp:8] -> [test.cpp:7] -> [test.cpp:5] -> [test.cpp:9] -> [test.cpp:3] -> [test.cpp:5]: (error) Using iterator to local container 'foo' that may be invalid.\n",
"[test.cpp:5] -> [test.cpp:7] -> [test.cpp:8] -> [test.cpp:8] -> [test.cpp:7] -> [test.cpp:5] -> [test.cpp:9] -> [test.cpp:3] -> [test.cpp:5]: (error, inconclusive) Using iterator to local container 'foo' that may be invalid.\n",
errout.str());
}
void eraseGoto() {

View File

@ -2603,6 +2603,23 @@ private:
" }\n"
"};";
ASSERT_EQUALS(true, testValueOfX(code, 6U, 10));
code = "void f(int i) {\n"
" if (i == 3) {}\n"
" for(int x = i;\n"
" x;\n"
" x++) {}\n"
"}\n";
ASSERT_EQUALS(true, testValueOfX(code, 4U, 3));
ASSERT_EQUALS(false, testValueOfXKnown(code, 4U, 3));
code = "void f() {\n"
" for(int x = 3;\n"
" x;\n"
" x++) {}\n"
"}\n";
ASSERT_EQUALS(true, testValueOfX(code, 4U, 3));
ASSERT_EQUALS(false, testValueOfXKnown(code, 4U, 3));
}
void valueFlowAfterSwap()
@ -3920,7 +3937,10 @@ private:
"void foo(struct S s) {\n"
" for (s.x = 0; s.x < 127; s.x++) {}\n"
"}";
values = removeImpossible(tokenValues(code, "<")); // TODO: comparison can be true or false
values = removeImpossible(tokenValues(code, "<"));
values.remove_if([&](const ValueFlow::Value& v) {
return !v.isKnown();
});
ASSERT_EQUALS(true, values.empty());
}
@ -4062,7 +4082,7 @@ private:
" for (int x = 0; x < 10 && y = do_something();)\n"
" x;\n"
"}";
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 4U, 0));
ASSERT_EQUALS(true, testValueOfX(code, 4U, 0));
code = "void f() {\n"
" int x,y;\n"