Address deprecated `mem::uninitialized` warnings in WASAPI host
This uses `ptr::null_mut()` and `MaybeUninit` to remove use of deprecated `mem::uninitialized()` which could possibly result in UB.
This commit is contained in:
parent
10e2458048
commit
a7008b63a5
|
@ -20,7 +20,7 @@ use std::ops::{Deref, DerefMut};
|
||||||
use std::os::windows::ffi::OsStringExt;
|
use std::os::windows::ffi::OsStringExt;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
use std::sync::{Arc, Mutex, MutexGuard, atomic::Ordering};
|
use std::sync::{Arc, Mutex, MutexGuard};
|
||||||
|
|
||||||
use super::check_result;
|
use super::check_result;
|
||||||
use super::check_result_backend_specific;
|
use super::check_result_backend_specific;
|
||||||
|
@ -196,7 +196,7 @@ impl DerefMut for WaveFormat {
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn immendpoint_from_immdevice(device: *const IMMDevice) -> *mut IMMEndpoint {
|
unsafe fn immendpoint_from_immdevice(device: *const IMMDevice) -> *mut IMMEndpoint {
|
||||||
let mut endpoint: *mut IMMEndpoint = mem::uninitialized();
|
let mut endpoint: *mut IMMEndpoint = ptr::null_mut();
|
||||||
check_result(
|
check_result(
|
||||||
(*device).QueryInterface(&IMMEndpoint::uuidof(), &mut endpoint as *mut _ as *mut _),
|
(*device).QueryInterface(&IMMEndpoint::uuidof(), &mut endpoint as *mut _ as *mut _),
|
||||||
)
|
)
|
||||||
|
@ -205,10 +205,10 @@ unsafe fn immendpoint_from_immdevice(device: *const IMMDevice) -> *mut IMMEndpoi
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn data_flow_from_immendpoint(endpoint: *const IMMEndpoint) -> EDataFlow {
|
unsafe fn data_flow_from_immendpoint(endpoint: *const IMMEndpoint) -> EDataFlow {
|
||||||
let mut data_flow = mem::uninitialized();
|
let mut data_flow = std::mem::MaybeUninit::<EDataFlow>::uninit();
|
||||||
check_result((*endpoint).GetDataFlow(&mut data_flow))
|
check_result((*endpoint).GetDataFlow(data_flow.as_mut_ptr()))
|
||||||
.expect("could not get endpoint data_flow");
|
.expect("could not get endpoint data_flow");
|
||||||
data_flow
|
data_flow.assume_init()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given the audio client and format, returns whether or not the format is supported.
|
// Given the audio client and format, returns whether or not the format is supported.
|
||||||
|
@ -354,7 +354,7 @@ impl Device {
|
||||||
let err = BackendSpecificError { description };
|
let err = BackendSpecificError { description };
|
||||||
return Err(err.into());
|
return Err(err.into());
|
||||||
}
|
}
|
||||||
let ptr_utf16 = *(&property_value.data as *const _ as *const (*const u16));
|
let ptr_utf16 = *(&property_value.data as *const _ as *const *const u16);
|
||||||
|
|
||||||
// Find the length of the friendly name.
|
// Find the length of the friendly name.
|
||||||
let mut len = 0;
|
let mut len = 0;
|
||||||
|
@ -395,7 +395,7 @@ impl Device {
|
||||||
}
|
}
|
||||||
|
|
||||||
let audio_client: *mut IAudioClient = unsafe {
|
let audio_client: *mut IAudioClient = unsafe {
|
||||||
let mut audio_client = mem::uninitialized();
|
let mut audio_client = ptr::null_mut();
|
||||||
let hresult = (*self.device).Activate(
|
let hresult = (*self.device).Activate(
|
||||||
&IID_IAudioClient,
|
&IID_IAudioClient,
|
||||||
CLSCTX_ALL,
|
CLSCTX_ALL,
|
||||||
|
@ -454,7 +454,7 @@ impl Device {
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
// Retrieve the pointer to the default WAVEFORMATEX.
|
// Retrieve the pointer to the default WAVEFORMATEX.
|
||||||
let mut default_waveformatex_ptr = WaveFormatExPtr(mem::uninitialized());
|
let mut default_waveformatex_ptr = WaveFormatExPtr(ptr::null_mut());
|
||||||
match check_result((*client).GetMixFormat(&mut default_waveformatex_ptr.0)) {
|
match check_result((*client).GetMixFormat(&mut default_waveformatex_ptr.0)) {
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||||
|
@ -571,7 +571,7 @@ impl Device {
|
||||||
let client = lock.unwrap().0;
|
let client = lock.unwrap().0;
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut format_ptr = WaveFormatExPtr(mem::uninitialized());
|
let mut format_ptr = WaveFormatExPtr(ptr::null_mut());
|
||||||
match check_result((*client).GetMixFormat(&mut format_ptr.0)) {
|
match check_result((*client).GetMixFormat(&mut format_ptr.0)) {
|
||||||
Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||||
return Err(DefaultFormatError::DeviceNotAvailable);
|
return Err(DefaultFormatError::DeviceNotAvailable);
|
||||||
|
@ -674,7 +674,7 @@ impl Device {
|
||||||
|
|
||||||
// obtaining the size of the samples buffer in number of frames
|
// obtaining the size of the samples buffer in number of frames
|
||||||
let max_frames_in_buffer = {
|
let max_frames_in_buffer = {
|
||||||
let mut max_frames_in_buffer = mem::uninitialized();
|
let mut max_frames_in_buffer = 0u32;
|
||||||
let hresult = (*audio_client).GetBufferSize(&mut max_frames_in_buffer);
|
let hresult = (*audio_client).GetBufferSize(&mut max_frames_in_buffer);
|
||||||
|
|
||||||
match check_result(hresult) {
|
match check_result(hresult) {
|
||||||
|
@ -717,7 +717,7 @@ impl Device {
|
||||||
// Building a `IAudioCaptureClient` that will be used to read captured samples.
|
// Building a `IAudioCaptureClient` that will be used to read captured samples.
|
||||||
let capture_client = {
|
let capture_client = {
|
||||||
let mut capture_client: *mut audioclient::IAudioCaptureClient =
|
let mut capture_client: *mut audioclient::IAudioCaptureClient =
|
||||||
mem::uninitialized();
|
ptr::null_mut();
|
||||||
let hresult = (*audio_client).GetService(
|
let hresult = (*audio_client).GetService(
|
||||||
&audioclient::IID_IAudioCaptureClient,
|
&audioclient::IID_IAudioCaptureClient,
|
||||||
&mut capture_client as *mut *mut audioclient::IAudioCaptureClient as *mut _,
|
&mut capture_client as *mut *mut audioclient::IAudioCaptureClient as *mut _,
|
||||||
|
@ -839,7 +839,7 @@ impl Device {
|
||||||
|
|
||||||
// obtaining the size of the samples buffer in number of frames
|
// obtaining the size of the samples buffer in number of frames
|
||||||
let max_frames_in_buffer = {
|
let max_frames_in_buffer = {
|
||||||
let mut max_frames_in_buffer = mem::uninitialized();
|
let mut max_frames_in_buffer = 0u32;
|
||||||
let hresult = (*audio_client).GetBufferSize(&mut max_frames_in_buffer);
|
let hresult = (*audio_client).GetBufferSize(&mut max_frames_in_buffer);
|
||||||
|
|
||||||
match check_result(hresult) {
|
match check_result(hresult) {
|
||||||
|
@ -861,7 +861,7 @@ impl Device {
|
||||||
|
|
||||||
// Building a `IAudioRenderClient` that will be used to fill the samples buffer.
|
// Building a `IAudioRenderClient` that will be used to fill the samples buffer.
|
||||||
let render_client = {
|
let render_client = {
|
||||||
let mut render_client: *mut audioclient::IAudioRenderClient = mem::uninitialized();
|
let mut render_client: *mut audioclient::IAudioRenderClient = ptr::null_mut();
|
||||||
let hresult = (*audio_client).GetService(
|
let hresult = (*audio_client).GetService(
|
||||||
&audioclient::IID_IAudioRenderClient,
|
&audioclient::IID_IAudioRenderClient,
|
||||||
&mut render_client as *mut *mut audioclient::IAudioRenderClient as *mut _,
|
&mut render_client as *mut *mut audioclient::IAudioRenderClient as *mut _,
|
||||||
|
@ -1021,7 +1021,7 @@ lazy_static! {
|
||||||
|
|
||||||
// building the devices enumerator object
|
// building the devices enumerator object
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut enumerator: *mut IMMDeviceEnumerator = mem::uninitialized();
|
let mut enumerator: *mut IMMDeviceEnumerator = ptr::null_mut();
|
||||||
|
|
||||||
let hresult = CoCreateInstance(
|
let hresult = CoCreateInstance(
|
||||||
&CLSID_MMDeviceEnumerator,
|
&CLSID_MMDeviceEnumerator,
|
||||||
|
@ -1062,7 +1062,7 @@ pub struct Devices {
|
||||||
impl Devices {
|
impl Devices {
|
||||||
pub fn new() -> Result<Self, DevicesError> {
|
pub fn new() -> Result<Self, DevicesError> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut collection: *mut IMMDeviceCollection = mem::uninitialized();
|
let mut collection: *mut IMMDeviceCollection = ptr::null_mut();
|
||||||
// can fail because of wrong parameters (should never happen) or out of memory
|
// can fail because of wrong parameters (should never happen) or out of memory
|
||||||
check_result_backend_specific((*ENUMERATOR.0).EnumAudioEndpoints(
|
check_result_backend_specific((*ENUMERATOR.0).EnumAudioEndpoints(
|
||||||
eAll,
|
eAll,
|
||||||
|
@ -1070,7 +1070,7 @@ impl Devices {
|
||||||
&mut collection,
|
&mut collection,
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
let count = mem::uninitialized();
|
let count = 0u32;
|
||||||
// can fail if the parameter is null, which should never happen
|
// can fail if the parameter is null, which should never happen
|
||||||
check_result_backend_specific((*collection).GetCount(&count))?;
|
check_result_backend_specific((*collection).GetCount(&count))?;
|
||||||
|
|
||||||
|
@ -1104,7 +1104,7 @@ impl Iterator for Devices {
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut device = mem::uninitialized();
|
let mut device = ptr::null_mut();
|
||||||
// can fail if out of range, which we just checked above
|
// can fail if out of range, which we just checked above
|
||||||
check_result((*self.collection).Item(self.next_item, &mut device)).unwrap();
|
check_result((*self.collection).Item(self.next_item, &mut device)).unwrap();
|
||||||
|
|
||||||
|
@ -1123,7 +1123,7 @@ impl Iterator for Devices {
|
||||||
|
|
||||||
fn default_device(data_flow: EDataFlow) -> Option<Device> {
|
fn default_device(data_flow: EDataFlow) -> Option<Device> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut device = mem::uninitialized();
|
let mut device = ptr::null_mut();
|
||||||
let hres = (*ENUMERATOR.0).GetDefaultAudioEndpoint(data_flow, eConsole, &mut device);
|
let hres = (*ENUMERATOR.0).GetDefaultAudioEndpoint(data_flow, eConsole, &mut device);
|
||||||
if let Err(_err) = check_result(hres) {
|
if let Err(_err) = check_result(hres) {
|
||||||
return None; // TODO: check specifically for `E_NOTFOUND`, and panic otherwise
|
return None; // TODO: check specifically for `E_NOTFOUND`, and panic otherwise
|
||||||
|
|
|
@ -260,7 +260,7 @@ fn wait_for_handle_signal(handles: &[winnt::HANDLE]) -> Result<usize, BackendSpe
|
||||||
// Get the number of available frames that are available for writing/reading.
|
// Get the number of available frames that are available for writing/reading.
|
||||||
fn get_available_frames(stream: &StreamInner) -> Result<u32, StreamError> {
|
fn get_available_frames(stream: &StreamInner) -> Result<u32, StreamError> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut padding = mem::uninitialized();
|
let mut padding = 0u32;
|
||||||
let hresult = (*stream.audio_client).GetCurrentPadding(&mut padding);
|
let hresult = (*stream.audio_client).GetCurrentPadding(&mut padding);
|
||||||
stream_error_from_hresult(hresult)?;
|
stream_error_from_hresult(hresult)?;
|
||||||
Ok(stream.max_frames_in_buffer - padding)
|
Ok(stream.max_frames_in_buffer - padding)
|
||||||
|
@ -371,8 +371,8 @@ fn process_input(
|
||||||
let mut frames_available = 0;
|
let mut frames_available = 0;
|
||||||
unsafe {
|
unsafe {
|
||||||
// Get the available data in the shared buffer.
|
// Get the available data in the shared buffer.
|
||||||
let mut buffer: *mut BYTE = mem::uninitialized();
|
let mut buffer: *mut BYTE = ptr::null_mut();
|
||||||
let mut flags = mem::uninitialized();
|
let mut flags = mem::MaybeUninit::uninit();
|
||||||
loop {
|
loop {
|
||||||
let hresult = (*capture_client).GetNextPacketSize(&mut frames_available);
|
let hresult = (*capture_client).GetNextPacketSize(&mut frames_available);
|
||||||
if let Err(err) = stream_error_from_hresult(hresult) {
|
if let Err(err) = stream_error_from_hresult(hresult) {
|
||||||
|
@ -385,7 +385,7 @@ fn process_input(
|
||||||
let hresult = (*capture_client).GetBuffer(
|
let hresult = (*capture_client).GetBuffer(
|
||||||
&mut buffer,
|
&mut buffer,
|
||||||
&mut frames_available,
|
&mut frames_available,
|
||||||
&mut flags,
|
flags.as_mut_ptr(),
|
||||||
ptr::null_mut(),
|
ptr::null_mut(),
|
||||||
ptr::null_mut(),
|
ptr::null_mut(),
|
||||||
);
|
);
|
||||||
|
@ -435,7 +435,7 @@ fn process_output(
|
||||||
};
|
};
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut buffer: *mut BYTE = mem::uninitialized();
|
let mut buffer: *mut BYTE = ptr::null_mut();
|
||||||
let hresult =
|
let hresult =
|
||||||
(*render_client).GetBuffer(frames_available, &mut buffer as *mut *mut _);
|
(*render_client).GetBuffer(frames_available, &mut buffer as *mut *mut _);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue