[UNITRACE] Fix CID 723248 invalid iterator dereference in ZeMetricProfiler#100
[UNITRACE] Fix CID 723248 invalid iterator dereference in ZeMetricProfiler#100
Conversation
Fix Coverity CID 723248: After advancing the iterator `kit` past the end and breaking from the innermost loop, the middle loop could continue and dereference the invalid iterator on the next iteration. Add a check after the inner loop to break out of the middle loop when `kit` reaches `kinfo.end()`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a Coverity-detected defect (CID 723248) involving an invalid iterator dereference in the ZeMetricProfiler class. The issue occurs when an iterator is advanced past the end of a container in an inner loop, then potentially dereferenced in subsequent iterations of the middle loop.
Key Changes:
- Added an iterator bounds check after the inner loop to prevent dereferencing an invalid iterator
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| value += samples[i]; | ||
| if (kit == kinfo.end()) { | ||
| break; | ||
| } |
There was a problem hiding this comment.
The check occurs after value += samples[i] at line 1158, but the iterator kit is used or incremented in the inner loop above. If kit was incremented to kinfo.end() in the inner loop and the code breaks from it, then continues to line 1158, there's a potential that kit might be dereferenced between the break point and this check. Verify that no dereference of kit occurs at line 1158 or ensure this check happens immediately after the inner loop before any potential use of kit.
| value += samples[i]; | |
| if (kit == kinfo.end()) { | |
| break; | |
| } | |
| if (kit == kinfo.end()) { | |
| break; | |
| } | |
| value += samples[i]; |
Fix Coverity CID 723248: After advancing the iterator
kitpast the end and breaking from the innermost loop, the middle loop could continue and dereference the invalid iterator on the next iteration.Add a check after the inner loop to break out of the middle loop when
kitreacheskinfo.end().🤖 Generated with Claude Code