Fix FN deallocuse with array access (#5121)

* Fix FN deallocuse with array access

* Fix another FN

* Undo
This commit is contained in:
chrchr-github 2023-06-08 07:55:49 +02:00 committed by GitHub
parent 901b775471
commit be2824b003
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -326,6 +326,9 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
if (!Token::Match(tok, "[;{},]") || Token::Match(tok->next(), "[;{},]"))
continue;
if (Token::Match(tok, "[;{},] %var% ["))
continue;
tok = tok->next();
if (!tok || tok == endToken)
break;
@ -1067,7 +1070,7 @@ void CheckLeakAutoVar::ret(const Token *tok, VarInfo &varInfo, const bool isEndO
tok2 = tok3->tokAt(4);
else
continue;
if (Token::Match(tok2, "[});,+]")) {
if (Token::Match(tok2, "[});,+[]")) {
used = true;
break;
}

View File

@ -111,6 +111,7 @@ private:
TEST_CASE(deallocuse7); // #6467, #6469, #6473
TEST_CASE(deallocuse8); // #1765
TEST_CASE(deallocuse9); // #9781
TEST_CASE(deallocuse10);
TEST_CASE(doublefree1);
TEST_CASE(doublefree2);
@ -825,6 +826,20 @@ private:
ASSERT_EQUALS("", errout.str());
}
void deallocuse10() {
check("void f(char* p) {\n"
" free(p);\n"
" p[0] = 10;\n"
"}\n");
ASSERT_EQUALS("[test.c:3]: (error) Dereferencing 'p' after it is deallocated / released\n", errout.str());
check("int f(int* p) {\n"
" free(p);\n"
" return p[1];\n"
"}\n");
ASSERT_EQUALS("[test.c:2] -> [test.c:3]: (error) Returning/dereferencing 'p' after it is deallocated / released\n", errout.str());
}
void doublefree1() { // #3895
check("void f(char *p) {\n"
" if (x)\n"