Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,14 @@ bool isTemporary(const Token* tok, const Library* library, bool unknown)
isTemporary(tok->astOperand2(), library);
if (Token::Match(tok, ",|::"))
return isTemporary(tok->astOperand2(), library);
if (tok->isCast() || (tok->isCpp() && isCPPCast(tok)))
return isTemporary(tok->astOperand2(), library);
if (tok->isCast()) {
const Token* op = tok->isBinaryOp() ? tok->astOperand2() : tok->astOperand1();
if (!tok->isBinaryOp() && Token::simpleMatch(op, "{")) // compound literal
return false;
if (tok->valueType() && tok->valueType()->reference != Reference::None)
return isTemporary(op, library);
return true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This return true also applies to casts to a pointer type. In isSameIteratorContainerExpression() (checkstl.cpp) the container expression is the cast itself, so the same C++ pointer cast now counts as a temporary container:

void f(void* p) { std::sort(static_cast<std::vector<int>*>(p)->begin(), static_cast<std::vector<int>*>(p)->end()); }

The PR now reports Iterators to containers from different expressions 'static_cast<std::vector<int>*>(p)' and 'static_cast<std::vector<int>*>(p)' are used together. [mismatchingContainerExpression]. Base gives no warning, and reinterpret_cast and const_cast behave the same way. The pointer itself is a prvalue, but the object it points to is not a temporary. One fix is to keep the old behaviour for pointer results, for example if (tok->valueType() && tok->valueType()->pointer > 0) return false; or recurse into the operand. A regression test in teststl would cover it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://trac.cppcheck.net/ticket/15302
Pointers can also be temporaries: int* const& f() { return (int*)0; }

}
if (findLambdaEndToken(tok) != nullptr)
return true;
if (Token::Match(tok, ".|[|++|--|%name%|%assign%"))
Expand Down
5 changes: 4 additions & 1 deletion lib/checkautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,10 @@ void CheckAutoVariablesImpl::checkVarLifetimeScope(const Token * start, const To
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(tok, mSettings, true)) {
if (!printInconclusive && lt.inconclusive)
continue;
const Variable* var = lt.token->variable();
const Token* varTok = lt.token;
while (varTok->isCast() && varTok->valueType() && varTok->valueType()->reference != Reference::None)
varTok = varTok->isBinaryOp() ? varTok->astOperand2() : varTok->astOperand1();
const Variable* var = varTok->variable();
if (var && !var->isGlobal() && !var->isStatic() && !var->isReference() && !var->isRValueReference() &&
isInScope(var->nameToken(), tok->scope())) {
errorReturnReference(tok, lt.errorPath, lt.inconclusive);
Expand Down
24 changes: 24 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class TestAutoVariables : public TestFixture {
TEST_CASE(returnReference28);
TEST_CASE(returnReference29);
TEST_CASE(returnReference30); // #15094
TEST_CASE(returnReference31);
TEST_CASE(returnReferenceFunction);
TEST_CASE(returnReferenceContainer);
TEST_CASE(returnReferenceLiteral);
Expand Down Expand Up @@ -1790,6 +1791,23 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void returnReference31() // #15291
{
check("int& f(int i) {\n"
" return (int&)i;\n"
"}\n"
"const int& g(char c) {\n"
" return (int)c;\n"
"}\n"
"const int& h(char c) {\n"
" return c;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:12]: (error) Reference to local variable returned. [returnReference]\n"
"[test.cpp:5:12]: (error) Reference to temporary returned. [returnTempReference]\n"
"[test.cpp:8:12]: (error) Reference to local variable returned. [returnReference]\n", // TODO: temporary
errout_str());
}

void returnReferenceFunction() {
check("int& f(int& a) {\n"
" return a;\n"
Expand Down Expand Up @@ -4429,6 +4447,12 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS(
"[test.cpp:3:12] -> [test.cpp:2:13] -> [test.cpp:4:16] -> [test.cpp:8:16] -> [test.cpp:8:16] -> [test.cpp:9:12]: (error) Using object that is a temporary. [danglingTemporaryLifetime]\n",
errout_str());

check("int f(int i) {\n"
" const int* p = &(int) { i };\n"
" return *p;\n"
"}\n", dinit(CheckOptions, $.cpp = false));
ASSERT_EQUALS("", errout_str());
}

void danglingLifetimeBorrowedMembers()
Expand Down
Loading