Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1250,8 +1250,15 @@ const Token* CheckUninitVar::isVariableUsage(bool cpp, const Token *vartok, cons
tok = tok->astParent();
}
if (Token::simpleMatch(tok->astParent(), "=")) {
if (astIsLhs(tok) && (alloc == ARRAY || !derefValue || !derefValue->astOperand1() || !derefValue->astOperand1()->isCast()))
return nullptr;
if (astIsLhs(tok)) {
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))
return nullptr;
}
Expand Down
14 changes: 14 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,20 @@ class TestUninitVar : public TestFixture {
" return i;\n"
"}\n");
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..
Expand Down