From 0f52fea84ac14a0c6c9146711c6c69833544b6aa Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 16 Jan 2025 13:40:02 +0100 Subject: [PATCH 1/3] Update testautovariables.cpp --- test/testautovariables.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 3ceb711c6de..d463fed767b 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -2864,6 +2864,13 @@ class TestAutoVariables : public TestFixture { " std::cerr << str;\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("auto f() {\n" // #11420 + " std::vector x;\n" + " std::vector::iterator it = x.begin();\n" + " return it;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2] -> [test.cpp:4]: (error) Returning iterator to local container 'x' that will be invalid when returning.\n", errout_str()); } void danglingLifetimeContainerView() From 08f8d6030aebd575e81de9bc685038828fc36484 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 16 Jan 2025 13:45:19 +0100 Subject: [PATCH 2/3] Update valueflow.cpp --- lib/valueflow.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index e8c2eee4971..d97cd4c62a7 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1792,11 +1792,12 @@ static bool isDifferentType(const Token* src, const Token* dst) } else { std::pair decl = Token::typeDecl(src); std::pair parentdecl = Token::typeDecl(dst); - if (isNotEqual(decl, parentdecl)) + const bool isCpp = (src && src->isCpp()) || (dst && dst->isCpp()); + if (isNotEqual(decl, parentdecl) && !(isCpp && (Token::simpleMatch(decl.first, "auto") || Token::simpleMatch(parentdecl.first, "auto")))) return true; - if (isNotEqual(decl, dst->valueType(), dst->isCpp())) + if (isNotEqual(decl, dst->valueType(), isCpp)) return true; - if (isNotEqual(parentdecl, src->valueType(), src->isCpp())) + if (isNotEqual(parentdecl, src->valueType(), isCpp)) return true; } return false; From 680806f923dd0d50e8ce9487c56fff7f552efa43 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 16 Jan 2025 15:16:13 +0100 Subject: [PATCH 3/3] Add test for #10995 --- test/testautovariables.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index d463fed767b..fe43f582325 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -3090,6 +3090,14 @@ class TestAutoVariables : public TestFixture { ASSERT_EQUALS( "[test.cpp:3] -> [test.cpp:2] -> [test.cpp:3]: (error) Returning pointer to local variable 'a' that will be invalid when returning.\n", errout_str()); + + check("std::string_view f() {\n" // #10995 + " char a[10]{};\n" + " return a;\n" + "}\n"); + ASSERT_EQUALS( + "[test.cpp:3] -> [test.cpp:2] -> [test.cpp:3]: (error) Returning pointer to local variable 'a' that will be invalid when returning.\n", + errout_str()); } void danglingLifetimeUniquePtr()