Fix #11766 FP uninitdata with cast (#5170)

This commit is contained in:
chrchr-github 2023-06-20 18:06:57 +02:00 committed by GitHub
parent 78c7e3351f
commit f96e3c9d84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -1250,8 +1250,15 @@ 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) && (alloc == ARRAY || !derefValue || !derefValue->astOperand1() || !derefValue->astOperand1()->isCast())) if (astIsLhs(tok)) {
return nullptr; if (alloc == ARRAY || !derefValue || !derefValue->isUnaryOp("*"))
return nullptr;
const Token* deref = derefValue->astOperand1();
while (deref && deref->isCast())
deref = deref->astOperand1();
if (deref == vartok)
return nullptr;
}
if (alloc != NO_ALLOC && astIsRhs(valueExpr)) if (alloc != NO_ALLOC && astIsRhs(valueExpr))
return nullptr; return nullptr;
} }

View File

@ -2066,6 +2066,20 @@ private:
" return i;\n" " return i;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: a\n", errout.str());
checkUninitVar("void* f(size_t n, int i) {\n" // #11766
" char* p = (char*)malloc(n);\n"
" *(int*)p = i;\n"
" return p;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void* f(size_t n, int i) {\n"
" char* p = (char*)malloc(n);\n"
" *(int*)(void*)p = i;\n"
" return p;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
// class / struct.. // class / struct..