Skip to content

Commit

Permalink
Fix #11420 FN returnDanglingLifetime with auto (regression) (#7228)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Jan 16, 2025
1 parent d777511 commit 5caaf9c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1792,11 +1792,12 @@ static bool isDifferentType(const Token* src, const Token* dst)
} else {
std::pair<const Token*, const Token*> decl = Token::typeDecl(src);
std::pair<const Token*, const Token*> 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;
Expand Down
15 changes: 15 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2864,6 +2864,13 @@ class TestAutoVariables : public TestFixture {
" std::cerr << str;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("auto f() {\n" // #11420
" std::vector<int> x;\n"
" std::vector<int>::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()
Expand Down Expand Up @@ -3083,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()
Expand Down

0 comments on commit 5caaf9c

Please sign in to comment.