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

Get rid of a lot of transmutes #700

Merged
merged 2 commits into from
Aug 17, 2017
Merged

Get rid of a lot of transmutes #700

merged 2 commits into from
Aug 17, 2017

Conversation

jonas-schievink
Copy link
Contributor

Most could be replaced by simple raw pointer casts (or even perfectly
safe coercions!).

cc #373

unsafe {
quotactl(quota::QuotaCmd(quota::Q_GETQUOTA, which), Some(special), id, mem::transmute(dqblk))
}
quotactl(quota::QuotaCmd(quota::Q_GETQUOTA, which), Some(special), id, dqblk as *mut _ as *mut c_char)
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need to cast twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because you can only cast a reference to a raw pointer to the exact same type

@@ -139,7 +139,7 @@ impl<'a> Iterator for CmsgIterator<'a> {
if buf.len() < sizeof_cmsghdr {
return None;
}
let cmsg: &cmsghdr = unsafe { mem::transmute(buf.as_ptr()) };
let cmsg: &'a cmsghdr = unsafe { &*(buf.as_ptr() as *const cmsghdr) };
Copy link
Member

Choose a reason for hiding this comment

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

why the &* ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To dereference the raw pointer, then get a reference to the value behind (the dereference doesn't copy the value out)

}

unsafe fn ffi_len(&mut self) -> *mut socklen_t {
mem::transmute(&mut self.len)
&mut self.len
Copy link
Member

Choose a reason for hiding this comment

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

teehee, this one was silly

Copy link
Contributor

@Susurrus Susurrus left a comment

Choose a reason for hiding this comment

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

This also needs a rebase since there are new platforms added to CI that this should be tested with.

@@ -368,7 +368,7 @@ impl<'a> Set<'a, usize> for SetUsize {
}

unsafe fn ffi_ptr(&self) -> *const c_void {
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't actually be unsafe, since they are creating raw pointers from safe references. Creating raw pointers is safe, dereferencing raw pointers is unsafe. I suggest we also change all of these functions that do that to not be unsafe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be more accurate in terms of safety to make the Get and Set traits unsafe instead of those functions.

Copy link
Contributor

Choose a reason for hiding this comment

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

That would be required, yes. Could you change those in a separate commit after the current changes? I'm pretty certain the other function in Get are also safe, but I don't feel like getting familiar with it enough here to suggest we change those as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, sorry, I misread. You suggest the traits themselves be unsafe? The docs say:

If unsafe code cannot reasonably expect to defend against a bad implementation of the trait, then marking the trait unsafe is a reasonable choice.

Given this and other topics around unsafe, it does appear that the traits should be unsafe and all the functions should be safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

unwrap and blank should probably stay unsafe: The caller must uphold safety for those, not the callee or implementor.

@Susurrus
Copy link
Contributor

This looks ready-to-go to me. There are no public API changes, so no CHANGELOG entry is needed.

@asomers Any last comments? Didn't know if you got all your questions resolved yet?

@Susurrus
Copy link
Contributor

@jonas-schievink Can you rebase this? Then I think it's ready for merge.

Most could be replaced by simple raw pointer casts (or even perfectly
safe coercions!).

cc #373
@Susurrus
Copy link
Contributor

@asomers I think this is ready for merge, are you alright with it?

@asomers
Copy link
Member

asomers commented Aug 17, 2017

Yep.

bors r+

bors bot added a commit that referenced this pull request Aug 17, 2017
700: Get rid of a lot of transmutes r=asomers

Most could be replaced by simple raw pointer casts (or even perfectly
safe coercions!).

cc #373

725: Match syntax of libc_bitflags! with bitflags! r=asomers

Also update a couple of constant declarations while we're at it.
@bors
Copy link
Contributor

bors bot commented Aug 17, 2017

Timed out (retrying...)

bors bot added a commit that referenced this pull request Aug 17, 2017
700: Get rid of a lot of transmutes r=asomers

Most could be replaced by simple raw pointer casts (or even perfectly
safe coercions!).

cc #373
@bors
Copy link
Contributor

bors bot commented Aug 17, 2017

@bors bors bot merged commit 9c9af2c into nix-rust:master Aug 17, 2017
@jonas-schievink jonas-schievink deleted the untransmute branch August 17, 2017 23:15
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.

3 participants