From 46f6a06e3b6fd0bbaf9a590c143a419665b6b724 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 15 Jul 2023 14:28:27 +0200 Subject: [PATCH] Fix #11828 False positive when getting a span via an out argument --- 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 2c6bdac7e31..cae64e52d68 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 0765d2271dc..c2de88c8824 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -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& 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[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()