Fix #9139 (leak when variable has const qualifier) (#3159)

Also, in the tests, change socket/close to resource to get error
messages which say "resource leak" instead of "memory leak".
This commit is contained in:
Rikard Falkeborn 2021-03-03 06:58:38 +01:00 committed by GitHub
parent 438584a49e
commit 42a41e8b41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -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% !!("))

View File

@ -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];"