Skip to content

fix(BiMap): use value-based storage to eliminate rehash pointer invalidation#501

Merged
Miou-zora merged 3 commits into
496-feature-internalize-jolt-bodyid-entity-mapping-inside-the-physics-pluginfrom
copilot/sub-pr-500
Mar 19, 2026
Merged

fix(BiMap): use value-based storage to eliminate rehash pointer invalidation#501
Miou-zora merged 3 commits into
496-feature-internalize-jolt-bodyid-entity-mapping-inside-the-physics-pluginfrom
copilot/sub-pr-500

Conversation

Copilot AI commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

BiMap stored raw pointers to keys inside each std::unordered_map as cross-references. Any insert or erase triggering a rehash invalidated those pointers, causing use-after-free bugs on lookup and removal.

Changes

  • BiMap.hpp: Replace pointer-based cross-reference maps with two independent value-copying maps:
    • std::unordered_map<TLeft, TRight> _leftToRight
    • std::unordered_map<TRight, TLeft> _rightToLeft
  • Update Add, Remove, Get, and Contains to work with stored values directly instead of dereferencing pointers.

Before:

std::unordered_map<TLeft, const TRight *> _leftToRight;  // pointer into _rightToLeft
std::unordered_map<TRight, const TLeft *> _rightToLeft;  // pointer into _leftToRight

auto aitr = this->_leftToRight.insert_or_assign(left, nullptr);
auto bp = &((*aitr.first).first);           // raw pointer to key — invalidated on rehash
auto bitr = this->_rightToLeft.insert_or_assign(right, bp).first;
auto ap = &((*bitr).first);
(*aitr.first).second = ap;

After:

std::unordered_map<TLeft, TRight> _leftToRight;  // value copy
std::unordered_map<TRight, TLeft> _rightToLeft;  // value copy

this->_leftToRight.insert_or_assign(left, right);
this->_rightToLeft.insert_or_assign(right, left);

💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

…rage to prevent rehash invalidation

Co-authored-by: Miou-zora <[email protected]>
Copilot AI changed the title [WIP] [WIP] Address feedback on jolt bodyid mapping in physics plugin PR fix(BiMap): use value-based storage to eliminate rehash pointer invalidation Mar 18, 2026
Copilot AI requested a review from Miou-zora March 18, 2026 21:57
…de-the-physics-plugin' into copilot/sub-pr-500
@Miou-zora Miou-zora marked this pull request as ready for review March 19, 2026 08:08
@Miou-zora Miou-zora merged commit cbdbc85 into 496-feature-internalize-jolt-bodyid-entity-mapping-inside-the-physics-plugin Mar 19, 2026
3 checks passed
@Miou-zora Miou-zora deleted the copilot/sub-pr-500 branch March 19, 2026 08:08
@Miou-zora Miou-zora restored the copilot/sub-pr-500 branch March 19, 2026 08:09
@sonarqubecloud

Copy link
Copy Markdown

@Miou-zora Miou-zora deleted the copilot/sub-pr-500 branch March 23, 2026 12:32
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