Skip to content

Commit

Permalink
Fix #11828 False positive when getting a span via an out argument
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jul 15, 2023
1 parent 9ad18f5 commit 46f6a06
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 0 additions & 3 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8326,9 +8326,6 @@ bool ValueFlow::isContainerSizeChanged(const Token* tok, int indirect, const Set
if (astIsLHS(tok) && Token::Match(tok->astParent(), "%assign%|<<"))
return true;
const Library::Container* container = tok->valueType()->container;
// Views cannot change container size
if (container->view)
return false;
if (astIsLHS(tok) && Token::simpleMatch(tok->astParent(), "["))
return container->stdAssociativeLike;
const Library::Container::Action action = astContainerAction(tok);
Expand Down
15 changes: 15 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,21 @@ class TestStl : public TestFixture {
" if (x.Set(42U)) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());

checkNormal("struct S { void g(std::span<int>& r) const; };\n" // #11828
"int f(const S& s) {\n"
" std::span<int> t;\n"
" s.g(t);\n"
" return t[0];\n"
"}\n");
ASSERT_EQUALS("", errout.str());

checkNormal("char h() {\n"
" std::string s;\n"
" std::string_view sv(s);\n"
" return s[0];\n"
"}\n");
TODO_ASSERT_EQUALS("test.cpp:4:error:Out of bounds access in expression 's[0]' because 's' is empty.\n", "", errout.str());
}

void outOfBoundsSymbolic()
Expand Down

0 comments on commit 46f6a06

Please sign in to comment.