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

Dereferencing Headless::joystick1 is UB #5

Closed
kpp opened this issue Mar 20, 2019 · 2 comments
Closed

Dereferencing Headless::joystick1 is UB #5

kpp opened this issue Mar 20, 2019 · 2 comments

Comments

@kpp
Copy link

kpp commented Mar 20, 2019

unsafe { (*self.joystick1).set_buttons(button_mask) };

https://doc.rust-lang.org/reference/behavior-considered-undefined.html : Dereferencing a null or dangling raw pointer.

Headless::joystick1/2 are initialized with:

impl Headless {
    fn command_load_rom(&mut self) {
        let mut record_tas = self.read_byte();
        let filename = self.read_length_string();
        let mut joystick1 = Box::new(Joystick::new());
        let mut joystick2 = Box::new(Joystick::new());
        self.joystick1 = &mut *joystick1;
        self.joystick2 = &mut *joystick2;
        ...
        // drop joystick1 joystick2
        // Headless::joystick1/2 are dangling pointers since joystick1 joystick2 are freed
    }
@MichaelBurge
Copy link
Owner

joystick1 and joystick2 aren't freed - their ownership is transferred to the Nes object created in the same function.

nes.rs even has this line:

mapper.map_address_space(0x4017, 0x4017, _joystick2, false); // TODO -- Transfers ownership of joystick2 so it isn't deallocated

because even though the second controller isn't used, its ownership needs to be transferred to prevent the exact problem you describe.

@kpp
Copy link
Author

kpp commented Mar 21, 2019

Right

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