diff --git a/src/checkother.cpp b/src/checkother.cpp index 0b7a24885..52efea464 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -867,13 +867,26 @@ void CheckOther::returnPointerToStackData() std::list 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() == "{") diff --git a/test/testother.cpp b/test/testother.cpp index 2353458b4..a01d84df3 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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..