From 775bfe39c08e8ca950688d40733eececde9f4e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 28 Nov 2026 21:49:39 +0100 Subject: [PATCH 1/2] Fix #11023 false positive: uninitdata with delete on unused allocated array --- lib/checkuninitvar.cpp | 3 +++ test/testuninitvar.cpp | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index ffdedd83d1d..020125bb5c6 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1253,6 +1253,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 + if (!derefValue && Token::simpleMatch(valueExpr->astParent(), "delete")) + return nullptr; } // Passing variable to function.. diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 787ede556ea..9eb0f70e57f 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1941,6 +1941,13 @@ 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()); + checkUninitVar("void f() {\n" " char *s = (char *)malloc(100);\n" " strcat(s, \"abc\");\n" From b5beb9592a69cffcdb2817470192aaf679045c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 30 Nov 2026 09:16:39 +0100 Subject: [PATCH 2/2] review --- lib/checkuninitvar.cpp | 25 +++++++++++++++++++-- test/testuninitvar.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 020125bb5c6..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,8 +1274,8 @@ 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 - if (!derefValue && Token::simpleMatch(valueExpr->astParent(), "delete")) + // 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; } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 9eb0f70e57f..938fc7843a9 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1948,6 +1948,57 @@ class TestUninitVar : public TestFixture { "}\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"