[Profiler] Fix UB in DebugInfoStore::Get(): re-acquire iterator after map insertion#8283
Closed
korniltsev-grafanista-yolo-vibecoder239 wants to merge 1 commit into
Conversation
… map insertion ParseModuleDebugInfo() inserts into _modulesInfo, which may rehash the unordered_map and invalidate all iterators. The old code reused a stale iterator obtained before the insertion — undefined behavior per the C++ standard. Re-acquire via find() after insertion and simplify the ternary. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
korniltsev-grafanista-yolo-vibecoder239
requested a review
from a team
as a code owner
March 10, 2026 11:40
gleocadie
reviewed
Apr 16, 2026
| } | ||
|
|
||
| ModuleDebugInfo& info = (it == _modulesInfo.cend()) ? _modulesInfo[moduleId] : it->second; | ||
| ModuleDebugInfo& info = it->second; |
Collaborator
There was a problem hiding this comment.
you should still check if it is cend or not. What if the ParseModuleDebugInfo failed ? (ex: file does not exist, wrong pdb....)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
In
DebugInfoStore::Get(),ParseModuleDebugInfo(moduleId)inserts into the_modulesInfounordered_map via_modulesInfo[moduleId], which may trigger a rehash that invalidates all iterators. The old code reused a stale iterator obtained before the insertion — undefined behavior per the C++ standard. Fix: re-acquire the iterator viafind()after insertion, and simplify the now-unnecessary ternary.Proof of UB
Per cppreference
unordered_map::insert:And
unordered_map::operator[]performs an insert if the key doesn't exist, with the same invalidation rules.In the original code:
Severity: Low. In practice this bug does not cause any observable problem. libstdc++ and libc++ use a node-based
unordered_mapimplementation where rehash re-links nodes into new buckets but never frees or moves the nodes themselves. Stale iterators still point to valid memory and theend()sentinel comparison happens to produce correct results. The fix is still warranted to eliminate the UB — a different stdlib implementation or future change could break this.Changes
profiler/src/ProfilerEngine/Datadog.Profiler.Native/DebugInfoStore.cpp: AfterParseModuleDebugInfo()inserts into the map, re-acquire the iterator withit = _modulesInfo.find(moduleId). Remove the ternary that compared the stale iterator.🤖 Generated with Claude Code