Fix #1175 uninitialized data: casted to 'int *' and dereferenced (#5008)

This commit is contained in:
chrchr-github 2023-05-29 15:29:53 +02:00 committed by GitHub
parent cf4334904c
commit ec2f00d7c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View File

@ -1244,7 +1244,7 @@ const Token* CheckUninitVar::isVariableUsage(bool cpp, const Token *vartok, cons
tok = tok->astParent(); tok = tok->astParent();
} }
if (Token::simpleMatch(tok->astParent(), "=")) { if (Token::simpleMatch(tok->astParent(), "=")) {
if (astIsLhs(tok)) if (astIsLhs(tok) && (alloc == ARRAY || !derefValue || !derefValue->astOperand1() || !derefValue->astOperand1()->isCast()))
return nullptr; return nullptr;
if (alloc != NO_ALLOC && astIsRhs(valueExpr)) if (alloc != NO_ALLOC && astIsRhs(valueExpr))
return nullptr; return nullptr;

View File

@ -2050,6 +2050,14 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// #1175
checkUninitVar("void f() {\n"
" int* p = new int;\n"
" *((int*)*p) = 42;\n"
" delete p;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str());
checkUninitVar("int f() {\n" // #10596 checkUninitVar("int f() {\n" // #10596
" int* a = new int;\n" " int* a = new int;\n"
" int i{};\n" " int i{};\n"