diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 3da2047b6..704cc4e49 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -316,6 +316,9 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, if (!tok || tok == endToken) break; + if (Token::Match(tok, "const %type%")) + tok = tok->tokAt(2); + // parse statement, skip to last member const Token *varTok = tok; while (Token::Match(varTok, "%name% ::|. %name% !!(")) diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 421bd664b..b6b39a1ff 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -41,7 +41,7 @@ private: settings.library.setalloc("malloc", id, -1); settings.library.setrealloc("realloc", id, -1); settings.library.setdealloc("free", id, 1); - while (!Library::ismemory(++id)); + while (!Library::isresource(++id)); settings.library.setalloc("socket", id, -1); settings.library.setdealloc("close", id, 1); while (!Library::isresource(++id)); @@ -81,6 +81,7 @@ private: TEST_CASE(assign19); TEST_CASE(assign20); // #9187 TEST_CASE(assign21); // #10186 + TEST_CASE(assign22); // #9139 TEST_CASE(isAutoDealloc); @@ -438,6 +439,18 @@ private: ASSERT_EQUALS("", errout.str()); } + void assign22() { // #9139 + check("void f(char tempFileName[256]) {\n" + " const int fd = socket(AF_INET, SOCK_PACKET, 0 );\n" + "}", true); + ASSERT_EQUALS("[test.cpp:3]: (error) Resource leak: fd\n", errout.str()); + + check("void f() {\n" + " const void * const p = malloc(10);\n" + "}", true); + ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: p\n", errout.str()); + } + void isAutoDealloc() { check("void f() {\n" " char *p = new char[100];"