diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index ffdedd83d1d..826a9eefb2e 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -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()) + continue; + if (!var->isPointer() && !var->typeStartToken()->isStandardType() && !var->typeStartToken()->isEnumType()) + continue; + 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(); @@ -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.. diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 787ede556ea..938fc7843a9 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -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"