Repository navigation
Conversation
| return member; | ||
| } | ||
|
|
||
| const Variable* getSingleMemberArrowWriteTarget(const Token* tok) |
There was a problem hiding this comment.
This is an AI review. Take it with a grain of salt and feel free to reject it by resolving the comment.
I am worried that the uninitvar part is tailored to the exact example in the ticket. I built the PR and tried small variations of it:
struct P2 { int m_place; int m_other; };
struct It2 { P2 m_ptr; P2* operator->() { return &m_ptr; } };
It2 f2() { It2 it; it->m_place = 0; it->m_other = 0; return it; } // still uninitvar + uninitStructMember
struct P3 { int m_place; };
struct It3 { P3 m_ptr; int m_extra; P3* operator->() { return &m_ptr; } };
It3 f3() { It3 it; it->m_place = 0; it.m_extra = 0; return it; } // still uninitvar + uninitStructMemberOnly the variant with exactly one member in both the iterator and the pointee is fixed. That adds about 80 lines of very specific shape matching to astutils, plus hooks in ValueFlowAnalyzer::analyzeMatch(), MemberExpressionAnalyzer and two places in checkuninitvar. That is a lot of special-casing in core code for one layout.
Maybe split the PR? The checkstl eraseDereference part is self-contained. For the uninit part, a more general approach could map it->x to it.m_ptr.x when operator-> is known to return &m_ptr, so the existing member tracking handles any layout. Alternatively, treat a call to a non-const user operator-> on an uninitialized object like other non-const member calls.
| // A member, free or friend operator& can change the returned address. | ||
| // Keep the proof independent of overload resolution for address-of. | ||
| if (std::any_of(scope->symdb.scopeList.cbegin(), scope->symdb.scopeList.cend(), [](const Scope& candidate) { | ||
| return candidate.functionMap.count("operator&") != 0; |
There was a problem hiding this comment.
This is an AI review. Take it with a grain of salt and feel free to reject it by resolving the comment.
Performance: this loops over the whole symdb.scopeList every time getSingleMemberArrowWriteTarget() gets this far. The function is called from ValueFlowAnalyzer::analyzeMatch(), from MemberExpressionAnalyzer::match() and twice from checkuninitvar, so the scan can repeat many times per file. If this is kept, maybe compute "has any operator&" once (checkstl.cpp in this PR already does a similar scan once per iterators() call).
|
|
||
| bool CheckUninitVarImpl::isMemberVariableAssignment(const Token *tok, const std::string &membervar) const | ||
| { | ||
| if (const Variable* member = getSingleMemberArrowWriteTarget(tok->astParent())) { |
|
Test results for commit 52edaae (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8888/ 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. |
|
It feels like this should be linked to a trac ticket. Do you want that we create it? |
|
There should probably be a trac ticket for this PR. Please create it, or ask us to create it. Then the PR title should be updated. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new correctness problems: no crash, hang or new false positive in my tests, and the new and changed unit tests pass. One new limit on the uninit part: singlePlainDataMember() only accepts a public member, so the usual iterator shape, a class with a private storage member, still gets uninitvar + uninitStructMember on it->value = 1; return it;. The equivalent write through (*it).value = 1 is also still reported. This adds to the concern in comments.md that the uninit part fits only the exact example from the ticket. The conservative cases I tried still warn as on main: conditional and loop writes, and passing the object by const reference on the right-hand side.
Testing
- Built
cppcheckandtestrunnerinpr/, andcppcheckinbase/, withmake -j8. Both built cleanly. ./testrunner TestUninitVar TestStl: 199 tests, 0 failures, 42 todos. The PR touches no Python CLI tests.- My own tests in
/work/scratch, run withcppcheck -q --inconclusive, base compared with PR:- Writes in
while (b), in aforloop and in aswitchcase, thenreturn it;: the PR changes the error to the expected conditional warningUninitialized variable: it.item. it->value = use(it);(const-ref read on the right-hand side) is still reported.it->value = 1; return it->value;andCursor other = it;after the write are now clean, which is correct.- Not handled: an iterator with a private member.
struct Item { int value; }; class Cursor { Item item; public: Item* operator->() { return &item; } Item& operator*() { return item; } Cursor& operator++(); }; Cursor f1() { Cursor it; it->value = 1; return it; }
pr/cppcheck -q --inconclusive b.cppgives the same output on base and PR:b.cpp:11:5: error: Uninitialized variable: it [uninitvar] b.cpp:12:12: error: Uninitialized struct member: it.item [uninitStructMember] Cursor it; (*it).value = 1; g(it);, with the same struct as above but with a public member, is still reported on the PR (c.cpp:16:7 uninitvar,c.cpp:17:7 uninitStructMember). Theit->valueform of the same code is now clean.
- Writes in
Automated review of 52edaae by Claude. The pull request was built and tested locally.
| if (var.isStatic()) | ||
| continue; | ||
| if (member || !var.isPublic() || var.isArray() || var.isPointer() || var.isReference() || | ||
| var.isRValueReference() || var.isVolatile() || var.hasDefault()) |
There was a problem hiding this comment.
AI review
!var->isPublic() turns down the most common iterator shape: a class whose storage member is private (class Cursor { Item item; public: Item* operator->() { return &item; } ... };). On that code it->value = 1; return it; still gives uninitvar + uninitStructMember, the same as on main. Accepting eClass at the top of singlePlainDataMember() therefore only helps classes that make the member public. Access control does not affect whether operator-> exposes the storage, so this condition looks unnecessary.
Default-constructed iterators that return
&ownedMemberfromoperator->can be used to initialize their storage, but the complete example in Trac #6572, comment 7 currently produceseraseDereference,uninitvar, anduninitStructMember. This change makes that complete example diagnostic-free.The iterator check recognizes an initial arrow access whose visible implementations directly return owned storage. Iterator classification, unary dereference checking, and post-erase invalidation remain intact. Unknown bodies, inherited/proxy arrows and overloaded address-of stay conservative.
The initialization analysis additionally recognizes standalone writes through a narrowly proven accessor: a local receiver has one plain owned member with one primitive scalar, and every arrow overload returns the same member's built-in address. This lets the existing flow distinguish initialization from read-modify-write without a generic non-const-call bailout. Nested expression writes, alias bindings, complex layouts and writes to captured outer objects in deferred lambdas retain existing handling.
Validation:
git diff --check, and a file-scoped equivalent of the project's self-check pass.Please assign this work to KiritoYG if needed and consider it under the published $10 bounty schedule. Please confirm eligibility and the supported payment route after acceptance and the required ticket closure. GitHub Sponsors is available if supported. No award or payment is being claimed.