Skip to content

Commit 6eb9e7a

Browse files
committed
refactor: improve safety of get_id() function, remove CFStringRef
1 parent 01a240d commit 6eb9e7a

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/host/coreaudio/macos/device.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ use std::time::{Duration, Instant};
4949
use super::property_listener::AudioObjectPropertyListener;
5050
use coreaudio::audio_unit::macos_helpers::get_device_name;
5151

52-
type CFStringRef = *mut std::os::raw::c_void;
53-
5452
/// Attempt to set the device sample rate to the provided rate.
5553
/// Return an error if the requested sample rate is not supported by the device.
5654
fn set_sample_rate(
@@ -412,8 +410,13 @@ impl Device {
412410
mScope: kAudioObjectPropertyScopeGlobal,
413411
mElement: kAudioObjectPropertyElementMain,
414412
};
415-
let mut uid: CFStringRef = std::ptr::null_mut();
416-
let data_size = size_of::<CFStringRef>() as u32;
413+
414+
// CFString is retained by the audio object, use wrap_under_get_rule
415+
let mut uid: *mut CFString = std::ptr::null_mut();
416+
let data_size = size_of::<*mut CFString>() as u32;
417+
418+
// SAFETY: AudioObjectGetPropertyData is documented to write a CFString pointer
419+
// for kAudioDevicePropertyDeviceUID. We check the status code before use.
417420
let status = unsafe {
418421
AudioObjectGetPropertyData(
419422
self.audio_device_id,
@@ -425,14 +428,16 @@ impl Device {
425428
)
426429
};
427430
check_os_status(status)?;
431+
432+
// SAFETY: We verified uid is non-null and the status was successful
428433
if !uid.is_null() {
429434
let uid_string =
430-
unsafe { CFString::wrap_under_get_rule(uid as *mut CFString).to_string() };
435+
unsafe { CFString::wrap_under_get_rule(uid).to_string() };
431436
Ok(DeviceId::CoreAudio(uid_string))
432437
} else {
433438
Err(DeviceIdError::BackendSpecific {
434439
err: BackendSpecificError {
435-
description: "Device UID not found".to_string(),
440+
description: "Device UID is null".to_string(),
436441
},
437442
})
438443
}

0 commit comments

Comments
 (0)