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

audio: fix device name use-after-free in AudioDevice::open #1012

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/sdl2/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,10 @@ impl<'a, Channel: AudioFormatNum> AudioQueue<Channel> {
Some(device) => Some(CString::new(device).unwrap()),
None => None
};
let device_ptr = device.map_or(ptr::null(), |s| s.as_ptr());
// Warning: map_or consumes its argument; `device.map_or()` would therefore consume the
// CString and drop it, making device_ptr a dangling pointer! To avoid that we downgrade
// device to an Option<&_> first.
let device_ptr = device.as_ref().map_or(ptr::null(), |s| s.as_ptr());

let iscapture_flag = 0;
let device_id = sys::SDL_OpenAudioDevice(
Expand Down Expand Up @@ -652,7 +655,10 @@ impl<CB: AudioCallback> AudioDevice<CB> {
Some(device) => Some(CString::new(device).unwrap()),
None => None
};
let device_ptr = device.map_or(ptr::null(), |s| s.as_ptr());
// Warning: map_or consumes its argument; `device.map_or()` would therefore consume the
// CString and drop it, making device_ptr a dangling pointer! To avoid that we downgrade
// device to an Option<&_> first.
let device_ptr = device.as_ref().map_or(ptr::null(), |s| s.as_ptr());

let iscapture_flag = if capture { 1 } else { 0 };
let device_id = sys::SDL_OpenAudioDevice(
Expand Down