Fix issue 9400: FP returnTempReference - triggered by cast (#2259)

This commit is contained in:
Paul Fultz II 2019-10-09 23:52:11 -05:00 committed by amai2012
parent 0c1e979af7
commit a5c6ae1e08
2 changed files with 19 additions and 0 deletions

View File

@ -228,6 +228,8 @@ bool isTemporary(bool cpp, const Token* tok)
return isTemporary(cpp, tok->astOperand1()) || isTemporary(cpp, tok->astOperand2()); return isTemporary(cpp, tok->astOperand1()) || isTemporary(cpp, tok->astOperand2());
if (Token::Match(tok, ",|::")) if (Token::Match(tok, ",|::"))
return isTemporary(cpp, tok->astOperand2()); return isTemporary(cpp, tok->astOperand2());
if (tok->isCast() || (cpp && isCPPCast(tok)))
return isTemporary(cpp, tok->astOperand2());
if (Token::Match(tok, "?|.|[|++|--|%name%|%assign%")) if (Token::Match(tok, "?|.|[|++|--|%name%|%assign%"))
return false; return false;
if (tok->isUnaryOp("*")) if (tok->isUnaryOp("*"))

View File

@ -105,6 +105,7 @@ private:
TEST_CASE(returnReference10); TEST_CASE(returnReference10);
TEST_CASE(returnReference11); TEST_CASE(returnReference11);
TEST_CASE(returnReference12); TEST_CASE(returnReference12);
TEST_CASE(returnReference13);
TEST_CASE(returnReferenceFunction); TEST_CASE(returnReferenceFunction);
TEST_CASE(returnReferenceContainer); TEST_CASE(returnReferenceContainer);
TEST_CASE(returnReferenceLiteral); TEST_CASE(returnReferenceLiteral);
@ -1213,6 +1214,22 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void returnReference13() {
check("std::vector<int> v;\n"
"void* vp = &v;\n"
"int& foo(size_t i) {\n"
" return ((std::vector<int>*)vp)->at(i);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("std::vector<int> v;\n"
"void* vp = &v;\n"
"int& foo(size_t i) {\n"
" return static_cast<std::vector<int>*>(vp)->at(i);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void returnReferenceFunction() { void returnReferenceFunction() {
check("int& f(int& a) {\n" check("int& f(int& a) {\n"
" return a;\n" " return a;\n"