From e4827cb3df421bdaea83a7676f05f81a6a4aa0ee Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 15 Jul 2023 17:53:40 +0200 Subject: [PATCH] Fix #11828 False positive when getting a span via an out argument (#5243) We didn't warn for the TODO even before this change. --- lib/valueflow.cpp | 3 --- test/teststl.cpp | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 2c6bdac7e..cae64e52d 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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); diff --git a/test/teststl.cpp b/test/teststl.cpp index e743d8e60..b58ea3072 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -908,6 +908,21 @@ private: " if (x.Set(42U)) {}\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + checkNormal("struct S { void g(std::span& r) const; };\n" // #11828 + "int f(const S& s) {\n" + " std::span 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[2];\n" + "}\n"); + TODO_ASSERT_EQUALS("test.cpp:4:error:Out of bounds access in expression 's[2]' because 's' is empty.\n", "", errout.str()); } void outOfBoundsSymbolic()