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

unsafe code guidelines for hw1? #14

Closed
tomtomjhj opened this issue Oct 14, 2019 · 4 comments
Closed

unsafe code guidelines for hw1? #14

tomtomjhj opened this issue Oct 14, 2019 · 4 comments

Comments

@tomtomjhj
Copy link
Member

While implementing ART I ran into several cases where I had to use unsafe codes like raw ptr deref, std::ptr:: functions, and

unsafe { &mut *(v as &_ as *const _ as *mut _ as &mut T) ... }

Now I'm getting use-after-free errors so I decided to clean things up. But I'm still not sure how much unsafety we need in this HW.

@tomtomjhj tomtomjhj changed the title unsafety guidelines for hw1 unsafe code guidelines for hw1? Oct 14, 2019
@jeehoonkang
Copy link
Member

ART is a pointer-based data structure, and in my opinion, you cannot avoid unsafe blocks altogether. I'd recommend you avoid unsafe blocks as much as possible, but you don't need to eradicate them.

I'd also recommend you debug your program with CLion or VSCode.

@tomtomjhj
Copy link
Member Author

I believe we should use UnsafeCell<NodeBox<V>> for cursor.child instead of &mut NodeBox<V> to avoid UB.
See https://rust-lang.github.io/rust-clippy/master/index.html#cast_ref_to_mut .

@jeehoonkang
Copy link
Member

While it is generally advisable to use UnsafeCell instead of raw pointers, replacing &mut Nodebox<V> with UnsafeCell<NodeBox<V>> is wrong: the former is essentially a pointer, while the latter is essentially a data.

@tomtomjhj
Copy link
Member Author

oh that's right. I guess I mixed this up with something unrelated.

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

No branches or pull requests

2 participants