Skip to content

#[deriving] and unsafe pointers are easy to get wrong #13032

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

Closed
huonw opened this issue Mar 20, 2014 · 3 comments · Fixed by #13108
Closed

#[deriving] and unsafe pointers are easy to get wrong #13032

huonw opened this issue Mar 20, 2014 · 3 comments · Fixed by #13108
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

Comments

@huonw
Copy link
Member

huonw commented Mar 20, 2014

In the following, the deriving does the wrong thing: it shallow copies the pointer when the semantics mean it should be making a new allocation and copying the data; there's no indication of this until runtime double-frees/segfaults.

/// Wrapper around an owned allocation
#[deriving(Clone)]
struct Struct {
     owned: *mut int
}

impl Struct {
     fn new(x: int) { Struct { owned: unsafe {cast::transmute(~x)} } }
}
impl Drop for Struct {
    fn drop(&mut self) {
         let _: ~int = unsafe {cast::transmute(self.owned)};
    }
}

Some possible resolutions:

  • do nothing: unsafe code is unsafe
  • have lints for deriving + unsafe pointers
  • have deriving completely disabled on unsafe pointers
  • remove the trait impls for raw pointers
@huonw huonw added the A-lint label Mar 20, 2014
@nikomatsakis
Copy link
Contributor

I favor a lint.

@huonw
Copy link
Member Author

huonw commented Mar 20, 2014

(I'm happy to mentor this.)

@huonw huonw added the E-mentor label Mar 20, 2014
@pongad
Copy link
Contributor

pongad commented Mar 21, 2014

I'm interested in doing this. I'll find @huonw on IRC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants