d-unsed / ruru

Native Ruby extensions written in Rust
MIT License
832 stars 40 forks source link

Fix use-after-free #4

Closed steveklabnik closed 8 years ago

steveklabnik commented 8 years ago

It was fun hacking on this with you in Vilnius!

d-unsed commented 8 years ago

Hey @steveklabnik! Yeah that was a cool hacking session 😄

I think one way to improve the solution is to move CString::new(name).unwrap() (without as_ptr() call) back to util module, for example

pub fn str_to_cstring(str: &str) -> CString {
    // moves an ownership of CString to the caller
    CString::new(name).unwrap()
}

// ... caller of str_as_cstring()
unsafe {
    rb_define_class(
        // CString must be still alive here
        str_to_cstring(class_name).as_ptr(),
        superclass
    )
}

This may help to keep the code DRY and to avoid duplications of manual CString handling across the project. I will continue investigating it 🔍

Thanks for contribution!