Fixed #9532 (False positive: Out of bounds access in expression 'v[0]' because 'v' is empty.)

This commit is contained in:
Daniel Marjamäki 2019-12-14 19:04:19 +01:00
parent 5e07528af5
commit bcfc5924fa
3 changed files with 14 additions and 3 deletions

View File

@ -5479,7 +5479,7 @@ static void valueFlowContainerForward(Token *tok, nonneg int containerId, ValueF
} }
if (Token::simpleMatch(tok, ") {") && Token::Match(tok->link()->previous(), "while|for|if (")) { if (Token::simpleMatch(tok, ") {") && Token::Match(tok->link()->previous(), "while|for|if (")) {
const Token *start = tok->next(); const Token *start = tok->next();
if (isContainerSizeChanged(containerId, start, start->link())) if (isContainerSizeChanged(containerId, start, start->link()) || isEscapeScope(start, nullptr))
break; break;
tok = start->link(); tok = start->link();
if (Token::simpleMatch(tok, "} else {")) { if (Token::simpleMatch(tok, "} else {")) {

View File

@ -4658,8 +4658,7 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void duplicateExpression10() void duplicateExpression10() {
{
// #9485 // #9485
check("int f() {\n" check("int f() {\n"
" const int a = 1;\n" " const int a = 1;\n"

View File

@ -4191,6 +4191,18 @@ private:
"}"; "}";
ASSERT_EQUALS(0U, tokenValues(code, "v . size ( )").size()); ASSERT_EQUALS(0U, tokenValues(code, "v . size ( )").size());
// if
code = "bool f(std::vector<int>&) {\n" // #9532
" return false;\n"
"}\n"
"int g() {\n"
" std::vector<int> v;\n"
" if (f(v) || v.empty())\n"
" return 0;\n"
" return v[0];\n"
"}\n";
ASSERT_EQUALS(0U, tokenValues(code, "v [ 0 ]").size());
// container size => yields // container size => yields
code = "void f() {\n" code = "void f() {\n"
" std::string s = \"abcd\";\n" " std::string s = \"abcd\";\n"