Skip to content
Closed
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
24 changes: 24 additions & 0 deletions lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,27 @@ static bool astIsRhs(const Token *tok)
return tok && tok->astParent() && tok == tok->astParent()->astOperand2();
}

// does the destructor of the pointed-to class read members that are not initialized by default?
static bool isDataReadInDestructor(const ValueType* vt, const Library& library)
{
if (!vt || vt->pointer != 1 || !vt->typeScope)
return false;
const Function* destructor = vt->typeScope->getDestructor();
if (!destructor || !destructor->functionScope)
return false;
const Scope* body = destructor->functionScope;
for (const Token* tok = body->bodyStart; tok != body->bodyEnd; tok = tok->next()) {
const Variable* var = tok->variable();
if (!var || var->scope() != vt->typeScope || var->isStatic() || var->hasDefault())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

Only the destructor body of the exact class and only its own members (var->scope() != vt->typeScope) are checked. Base reports all of these and the PR reports none of them:

struct B { int x; ~B() { if (x) {} } };
struct D : B { int y; };
void f1() { D* d = new D; delete d; }
struct E : B { int y; ~E() {} };
void f2() { E* e = new E; delete e; }
struct T { int x; void g() { if (x) {} } ~T() { g(); } };
void f4() { T* t = new T; delete t; }
struct V { int x; ~V() { if (x) {} } };
struct Z { V v; ~Z() { } };
void f10() { Z* p = new Z; delete p; }

To cover these, the function could recurse into the destructors of base classes and of class-type members, and return true conservatively when the destructor calls a member function.

continue;
if (!var->isPointer() && !var->typeStartToken()->isStandardType() && !var->typeStartToken()->isEnumType())
continue;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The loop returns true on the first token that looks like a read. It does not notice that the member was written earlier in the destructor:

struct S { int x; ~S() { x = 0; if (x) {} } };
void f1() { S* p = new S; delete p; }

The PR reports Memory is allocated but not initialized: p [uninitdata] here. Base reported it too, so it is not a regression. One way to avoid the warning is to stop scanning a member once it has been assigned.

if (CheckUninitVarImpl::isVariableUsage(tok, library, var->isPointer(), CheckUninitVarImpl::NO_ALLOC))
return true;
}
return false;
}

const Token* CheckUninitVarImpl::isVariableUsage(const Token *vartok, const Library& library, bool pointer, Alloc alloc, int indirect)
{
const bool cpp = vartok->isCpp();
Expand Down Expand Up @@ -1253,6 +1274,9 @@ const Token* CheckUninitVarImpl::isVariableUsage(const Token *vartok, const Libr
return nullptr;
if (Token::simpleMatch(valueExpr->astParent(), "return"))
return nullptr;
// deallocating the memory does not read the data, but the destructor might do that
if (Token::simpleMatch(valueExpr->astParent(), "delete") && !isDataReadInDestructor(valueExpr->valueType(), library))
return nullptr;
}

// Passing variable to function..
Expand Down
58 changes: 58 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,64 @@ class TestUninitVar : public TestFixture {

// alloc..
void uninitvar_alloc() {
checkUninitVar("void f()\n" // #11023
"{\n"
" char * c = new char[12];\n"
" delete [] c;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

// delete => destructor reads uninitialized member
checkUninitVar("struct S { int x; ~S() { if (x) {} } };\n"
"void f() {\n"
" S* s = new S;\n"
" delete s;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:12]: (error) Memory is allocated but not initialized: s [uninitdata]\n", errout_str());

checkUninitVar("struct S { int x; ~S() { if (x) {} } };\n"
"void f() {\n"
" S* s = new S[2];\n"
" delete [] s;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:15]: (error) Memory is allocated but not initialized: s [uninitdata]\n", errout_str());

checkUninitVar("struct S { char* p; ~S() { delete [] p; } };\n"
"void f() {\n"
" S* s = new S;\n"
" delete s;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:12]: (error) Memory is allocated but not initialized: s [uninitdata]\n", errout_str());

// delete => destructor does not read uninitialized member
checkUninitVar("struct S { int x; };\n"
"void f() {\n"
" S* s = new S;\n"
" delete s;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

checkUninitVar("struct S { int x; ~S() {} };\n"
"void f() {\n"
" S* s = new S;\n"
" delete s;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

checkUninitVar("struct S { int x; ~S() { x = 0; } };\n"
"void f() {\n"
" S* s = new S;\n"
" delete s;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

checkUninitVar("struct S { int x; ~S(); };\n"
"void f() {\n"
" S* s = new S;\n"
" delete s;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

checkUninitVar("void f() {\n"
" char *s = (char *)malloc(100);\n"
" strcat(s, \"abc\");\n"
Expand Down
Loading