return pointer to local array: fixed false positive when return value is converted to for instance a std::string (#255)

This commit is contained in:
Daniel Marjamäki 2009-04-19 16:47:54 +02:00
parent 7cddc52d8f
commit ca8f25fced
2 changed files with 28 additions and 4 deletions

View File

@ -867,13 +867,26 @@ void CheckOther::returnPointerToStackData()
std::list<unsigned int> arrayVar;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (!infunc && Token::simpleMatch(tok, ") {"))
// Is there a function declaration for a function that returns a pointer?
if (!infunc && (Token::Match(tok, "%type% * %var% (") || Token::Match(tok, "%type% * %var% :: %var% (")))
{
infunc = true;
indentlevel = 0;
arrayVar.clear();
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
{
if (tok2->str() == ")")
{
tok = tok2;
break;
}
}
if (Token::simpleMatch(tok, ") {"))
{
infunc = true;
indentlevel = 0;
arrayVar.clear();
}
}
// Parsing a function that returns a pointer..
if (infunc)
{
if (tok->str() == "{")

View File

@ -53,6 +53,7 @@ private:
TEST_CASE(strPlusChar3); // ok: path + "/sub" + '/'
TEST_CASE(returnLocalVariable1);
TEST_CASE(returnLocalVariable2);
TEST_CASE(varScope1);
TEST_CASE(varScope2);
@ -309,6 +310,16 @@ private:
ASSERT_EQUALS(std::string("[test.cpp:4]: (error) Returning pointer to local array variable\n"), errout.str());
}
void returnLocalVariable2()
{
retVar("std::string foo()\n"
"{\n"
" char str[100] = {0};\n"
" return str;\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
void varScope(const char code[])
{
// Tokenize..