Skip to content

Commit 60b2b65

Browse files
committed
Support new/delete in new uninitialized variable checking; realloc() requires an initialized first argument.
1 parent c3e47f7 commit 60b2b65

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎cfg/std.cfg‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4103,6 +4103,9 @@
41034103
<!-- void *realloc(void *block, size_t newsize); -->
41044104
<function name="realloc">
41054105
<noreturn>false</noreturn>
4106+
<arg nr="1">
4107+
<not-uninit/>
4108+
</arg>
41064109
<arg nr="2">
41074110
<not-uninit/>
41084111
</arg>

‎lib/checkuninitvar.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,11 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
14951495
*alloc = true;
14961496
continue;
14971497
}
1498+
if (var.isPointer() && (_tokenizer->isC() || var.typeStartToken()->isStandardType() || (var.type() && var.type()->needInitialization == Type::True)) && Token::Match(tok->next(), "= new")) {
1499+
if (alloc)
1500+
*alloc = true;
1501+
continue;
1502+
}
14981503

14991504

15001505
if (!membervar.empty()) {
@@ -1676,7 +1681,7 @@ void CheckUninitVar::checkRhs(const Token *tok, const Variable &var, bool alloc,
16761681

16771682
bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool alloc) const
16781683
{
1679-
if (!alloc && vartok->previous()->str() == "return" && vartok->strAt(1) != "=")
1684+
if (!alloc && ((Token::Match(vartok->previous(), "return|delete") && vartok->strAt(1) != "=") || (vartok->strAt(-1) == "]" && vartok->linkAt(-1)->strAt(-1) == "delete")))
16801685
return true;
16811686

16821687
// Passing variable to typeof/__alignof__

0 commit comments

Comments
 (0)