Repository navigation
Fix #11023 false positive: uninitdata with delete on unused allocated array #9089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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.. | ||
|
|
||
There was a problem hiding this comment.
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: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.