Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validity problem #96

Merged
merged 1 commit into from
Aug 16, 2019
Merged

Fix validity problem #96

merged 1 commit into from
Aug 16, 2019

Conversation

MikailBag
Copy link
Contributor

This PR addresses two problems.

  • mem::uninitialized is going to be deprecated.
  • When K or V type has niche (e.g. bool or char), creating Node with mem::uninitialized() has undefined behavior.

@FlashCat
Copy link
Contributor

Thanks for the pull request, and welcome! The contain-rs team is excited to review your changes, and you should hear from @reem (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

@Gankra
Copy link
Contributor

Gankra commented Aug 16, 2019

Yikes. Ok so this is definitely an improvement but I think more needs to be done to make this rigorously correct. In particular it is very easy to accidentally assert that the K and V exist transiently, leading to technical but unlikely to matter UB.

I believe the correct fix here is a more subtle and frankly very annoying, in the same vein as these fixes we did for BTreeMap and LinkedList.

Copy link
Contributor

@Gankra Gankra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth landing regardless as a step in the right direction

@Gankra Gankra merged commit 4485163 into contain-rs:master Aug 16, 2019
@Gankra
Copy link
Contributor

Gankra commented Aug 16, 2019

see #97 for hasty notes

@@ -171,7 +171,8 @@ impl<K, V, S> LinkedHashMap<K, V, S> {
if self.head.is_null() {
// allocate the guard node if not present
unsafe {
self.head = Box::into_raw(Box::new(mem::uninitialized()));
let node_layout = std::alloc::Layout::new::<Node<K, V>>();
self.head = std::alloc::alloc(node_layout) as *mut Node<K, V>;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that alloc::alloc is "expected to be deprecated".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah but I doubt they'll actually do that since it's strictly worse

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although Layout also blows so what do I know

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.

None yet

5 participants