Skip to content

Fix #11023 false positive: uninitdata with delete on unused allocated array - #9089

Open
danmar wants to merge 1 commit into
cppcheck-opensource:mainfrom
cppchecksolutions:fix-11023
Open

danmar wants to merge 1 commit into
cppcheck-opensource:mainfrom
cppchecksolutions:fix-11023

Conversation

@danmar

@danmar danmar commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

@dmcppcheck dmcppcheck left a comment

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 fix works for the reported case (#11023) and for variants such as delete c, delete[] (c) and delete[] (c+1). True positives where the data is read (delete c[0], delete[] *c, delete s->p, an uninitialized pointer) are still reported. One new false negative: the check skips every delete of a non-dereferenced pointer, so it no longer warns when the destructor of the deleted type reads members that were never initialized (struct V { int x; ~V(){ if (x) {} } }; V* v = new V; delete v;). Base reported that case. I found no false positives or crashes.

Comments

# Comment By Where Status
1 delete of a class whose destructor reads uninitialized members is no longer reported AI review lib/checkuninitvar.cpp:1257 🆕 New (false negative)

@danmar, please fix, resolve or reply to the open and new comments: #1.
For #1 (false negative), it is enough to create a Trac ticket and link it here. It does not need to be fixed in this PR.

Testing

Built cppcheck and testrunner in pr/, and cppcheck in base/; both builds succeeded. ./testrunner TestUninitVar: 70 tests, 0 failed.

I compared base and PR on scratch files with cppcheck -q --enable=all --inconclusive:

  • False positive gone: new char[12]; delete[] c;, new char; delete c;, delete[] (c); and delete[] (c+1);.
  • Still reported, as in base: delete c[0], delete[] *c, delete s->p, a read of c[0] or *c before the delete, and an uninitialized pointer passed to delete.

New false negative:

struct U { int x; ~U(); };
void g1() { U* u = new U; delete u; }
struct V { int x; ~V() { if (x) {} } };
void g2() { V* v = new V; delete v; }
void g3() { V* v = new V[2]; delete [] v; }

cppcheck -q --enable=warning b.cpp

  • base: uninitdata on lines 2, 4 and 5
  • PR: no output

Not run: the full test suite and the Python tests in test/cli (the PR doesn't touch them).


Automated review of 775bfe3 by Claude. The pull request was built and tested locally.

Comment thread lib/checkuninitvar.cpp
if (Token::simpleMatch(valueExpr->astParent(), "return"))
return nullptr;
// deallocating the memory does not read the data
if (!derefValue && Token::simpleMatch(valueExpr->astParent(), "delete"))

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

This returns nullptr for every delete p where p is not dereferenced, whatever the pointed-to type is. For a class with a user-written destructor that reads members, the delete does read the uninitialized data:

struct V { int x; ~V() { if (x) {} } };
void g2() { V* v = new V; delete v; }
void g3() { V* v = new V[2]; delete [] v; }

Base reports uninitdata for both functions; the PR reports nothing. One option is to skip only when the pointed-to type is trivially destructible, i.e. it is not a class with a user-declared destructor. A test for this case would also help.

@dmcppcheck

Copy link
Copy Markdown
Contributor

Test results for commit 775bfe3 (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9089/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: +0.1% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9089/2026-11-29_17-58-19-775bfe39c08e_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants