Fix UB in retain_with_order()#48
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes UB in LinkedHashMap::retain_with_order() when keys can mutate their hash/equality via interior mutability during the user callback (issue #42), preventing the hash table from ending up pointing at a freed/moved-out node.
Changes:
- In
retain_with_order(), compute the node’s hash before invoking the callback and remove the hash-table entry by pointer identity (not key equality) to ensure the removed table entry matches the freed list node. - Add a regression test that triggers the reported scenario (key mutated to compare equal to another key inside the callback) and exercises a subsequent lookup under miri.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/linked_hash_map.rs |
Makes retain_with_order() remove the exact node being filtered (pre-hash + pointer-identity match) to avoid stale table pointers / UB. |
tests/linked_hash_map.rs |
Adds a regression test covering the interior-mutability key mutation case from issue #42. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+924
to
+925
| // Looking up a stale key must not dereference a freed node. | ||
| let _ = map.contains_key(&Key::new("a")); |
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.
Fixes #42.