Skip to content

Commit 811fc0c

Browse files
committed
Fix cppcheck-opensource#1175 uninitialized data: casted to 'int *' and dereferenced
1 parent ad464c4 commit 811fc0c

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

lib/checkuninitvar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ const Token* CheckUninitVar::isVariableUsage(bool cpp, const Token *vartok, cons
12401240
tok = tok->astParent();
12411241
}
12421242
if (Token::simpleMatch(tok->astParent(), "=")) {
1243-
if (astIsLhs(tok))
1243+
if (astIsLhs(tok) && (alloc == ARRAY || !derefValue || !derefValue->astOperand1() || !derefValue->astOperand1()->isCast()))
12441244
return nullptr;
12451245
if (alloc != NO_ALLOC && astIsRhs(valueExpr))
12461246
return nullptr;

test/testuninitvar.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,14 @@ class TestUninitVar : public TestFixture {
20492049
" A* a = new A{};\n"
20502050
"}\n");
20512051
ASSERT_EQUALS("", errout.str());
2052+
2053+
// #1175
2054+
checkUninitVar("void f() {\n"
2055+
" int* p = new int;\n"
2056+
" *((int*)*p) = 42;\n"
2057+
" delete p;\n"
2058+
"}\n");
2059+
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str());
20522060
}
20532061

20542062
// class / struct..

0 commit comments

Comments
 (0)