Fix #8174 Regression: false negative autovarInvalidDeallocation since 1.79 (#5045)

This commit is contained in:
chrchr-github 2023-05-28 14:34:07 +02:00 committed by GitHub
parent 774123d28d
commit 4c1e06a84b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -277,9 +277,10 @@ void CheckAutoVariables::autoVariables()
errorInvalidDeallocation(tok, nullptr);
else if (tok->variable() && tok->variable()->isPointer()) {
for (const ValueFlow::Value &v : tok->values()) {
if (!(v.isTokValue()))
if (v.isImpossible())
continue;
if (isArrayVar(v.tokvalue) || ((v.tokvalue->tokType() == Token::eString) && !v.isImpossible())) {
if ((v.isTokValue() && (isArrayVar(v.tokvalue) || ((v.tokvalue->tokType() == Token::eString)))) ||
(v.isLocalLifetimeValue() && v.lifetimeKind == ValueFlow::Value::LifetimeKind::Address)) {
errorInvalidDeallocation(tok, &v);
break;
}

View File

@ -793,6 +793,23 @@ private:
"};\n"
"Array arr;\n");
ASSERT_EQUALS("", errout.str());
// #8174
check("struct S {};\n"
"void f() {\n"
" S s;\n"
" S* p = &s;\n"
" free(p);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Deallocation of an auto-variable (s) results in undefined behaviour.\n", errout.str());
check("void f(bool b, int* q) {\n"
" int i;\n"
" int* p = b ? &i : q;\n"
" if (!b)\n"
" free(p);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void testinvaliddealloc_input() {