Skip to content

Commit b5beb95

Browse files
committed
review
1 parent 775bfe3 commit b5beb95

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎lib/checkuninitvar.cpp‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,27 @@ static bool astIsRhs(const Token *tok)
11531153
return tok && tok->astParent() && tok == tok->astParent()->astOperand2();
11541154
}
11551155

1156+
// does the destructor of the pointed-to class read members that are not initialized by default?
1157+
static bool isDataReadInDestructor(const ValueType* vt, const Library& library)
1158+
{
1159+
if (!vt || vt->pointer != 1 || !vt->typeScope)
1160+
return false;
1161+
const Function* destructor = vt->typeScope->getDestructor();
1162+
if (!destructor || !destructor->functionScope)
1163+
return false;
1164+
const Scope* body = destructor->functionScope;
1165+
for (const Token* tok = body->bodyStart; tok != body->bodyEnd; tok = tok->next()) {
1166+
const Variable* var = tok->variable();
1167+
if (!var || var->scope() != vt->typeScope || var->isStatic() || var->hasDefault())
1168+
continue;
1169+
if (!var->isPointer() && !var->typeStartToken()->isStandardType() && !var->typeStartToken()->isEnumType())
1170+
continue;
1171+
if (CheckUninitVarImpl::isVariableUsage(tok, library, var->isPointer(), CheckUninitVarImpl::NO_ALLOC))
1172+
return true;
1173+
}
1174+
return false;
1175+
}
1176+
11561177
const Token* CheckUninitVarImpl::isVariableUsage(const Token *vartok, const Library& library, bool pointer, Alloc alloc, int indirect)
11571178
{
11581179
const bool cpp = vartok->isCpp();
@@ -1253,8 +1274,8 @@ const Token* CheckUninitVarImpl::isVariableUsage(const Token *vartok, const Libr
12531274
return nullptr;
12541275
if (Token::simpleMatch(valueExpr->astParent(), "return"))
12551276
return nullptr;
1256-
// deallocating the memory does not read the data
1257-
if (!derefValue && Token::simpleMatch(valueExpr->astParent(), "delete"))
1277+
// deallocating the memory does not read the data, but the destructor might do that
1278+
if (Token::simpleMatch(valueExpr->astParent(), "delete") && !isDataReadInDestructor(valueExpr->valueType(), library))
12581279
return nullptr;
12591280
}
12601281

‎test/testuninitvar.cpp‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,57 @@ class TestUninitVar : public TestFixture {
19481948
"}\n");
19491949
ASSERT_EQUALS("", errout_str());
19501950

1951+
// delete => destructor reads uninitialized member
1952+
checkUninitVar("struct S { int x; ~S() { if (x) {} } };\n"
1953+
"void f() {\n"
1954+
" S* s = new S;\n"
1955+
" delete s;\n"
1956+
"}\n");
1957+
ASSERT_EQUALS("[test.cpp:4:12]: (error) Memory is allocated but not initialized: s [uninitdata]\n", errout_str());
1958+
1959+
checkUninitVar("struct S { int x; ~S() { if (x) {} } };\n"
1960+
"void f() {\n"
1961+
" S* s = new S[2];\n"
1962+
" delete [] s;\n"
1963+
"}\n");
1964+
ASSERT_EQUALS("[test.cpp:4:15]: (error) Memory is allocated but not initialized: s [uninitdata]\n", errout_str());
1965+
1966+
checkUninitVar("struct S { char* p; ~S() { delete [] p; } };\n"
1967+
"void f() {\n"
1968+
" S* s = new S;\n"
1969+
" delete s;\n"
1970+
"}\n");
1971+
ASSERT_EQUALS("[test.cpp:4:12]: (error) Memory is allocated but not initialized: s [uninitdata]\n", errout_str());
1972+
1973+
// delete => destructor does not read uninitialized member
1974+
checkUninitVar("struct S { int x; };\n"
1975+
"void f() {\n"
1976+
" S* s = new S;\n"
1977+
" delete s;\n"
1978+
"}\n");
1979+
ASSERT_EQUALS("", errout_str());
1980+
1981+
checkUninitVar("struct S { int x; ~S() {} };\n"
1982+
"void f() {\n"
1983+
" S* s = new S;\n"
1984+
" delete s;\n"
1985+
"}\n");
1986+
ASSERT_EQUALS("", errout_str());
1987+
1988+
checkUninitVar("struct S { int x; ~S() { x = 0; } };\n"
1989+
"void f() {\n"
1990+
" S* s = new S;\n"
1991+
" delete s;\n"
1992+
"}\n");
1993+
ASSERT_EQUALS("", errout_str());
1994+
1995+
checkUninitVar("struct S { int x; ~S(); };\n"
1996+
"void f() {\n"
1997+
" S* s = new S;\n"
1998+
" delete s;\n"
1999+
"}\n");
2000+
ASSERT_EQUALS("", errout_str());
2001+
19512002
checkUninitVar("void f() {\n"
19522003
" char *s = (char *)malloc(100);\n"
19532004
" strcat(s, \"abc\");\n"

0 commit comments

Comments
 (0)