Skip to content

Commit bcfc592

Browse files
committed
Fixed #9532 (False positive: Out of bounds access in expression 'v[0]' because 'v' is empty.)
1 parent 5e07528 commit bcfc592

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

lib/valueflow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5479,7 +5479,7 @@ static void valueFlowContainerForward(Token *tok, nonneg int containerId, ValueF
54795479
}
54805480
if (Token::simpleMatch(tok, ") {") && Token::Match(tok->link()->previous(), "while|for|if (")) {
54815481
const Token *start = tok->next();
5482-
if (isContainerSizeChanged(containerId, start, start->link()))
5482+
if (isContainerSizeChanged(containerId, start, start->link()) || isEscapeScope(start, nullptr))
54835483
break;
54845484
tok = start->link();
54855485
if (Token::simpleMatch(tok, "} else {")) {

test/testother.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4658,8 +4658,7 @@ class TestOther : public TestFixture {
46584658
ASSERT_EQUALS("", errout.str());
46594659
}
46604660

4661-
void duplicateExpression10()
4662-
{
4661+
void duplicateExpression10() {
46634662
// #9485
46644663
check("int f() {\n"
46654664
" const int a = 1;\n"

test/testvalueflow.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4191,6 +4191,18 @@ class TestValueFlow : public TestFixture {
41914191
"}";
41924192
ASSERT_EQUALS(0U, tokenValues(code, "v . size ( )").size());
41934193

4194+
// if
4195+
code = "bool f(std::vector<int>&) {\n" // #9532
4196+
" return false;\n"
4197+
"}\n"
4198+
"int g() {\n"
4199+
" std::vector<int> v;\n"
4200+
" if (f(v) || v.empty())\n"
4201+
" return 0;\n"
4202+
" return v[0];\n"
4203+
"}\n";
4204+
ASSERT_EQUALS(0U, tokenValues(code, "v [ 0 ]").size());
4205+
41944206
// container size => yields
41954207
code = "void f() {\n"
41964208
" std::string s = \"abcd\";\n"

0 commit comments

Comments
 (0)