Skip to content

Commit

Permalink
Merge pull request #545 from b-ma/fix/invalid_sink_id
Browse files Browse the repository at this point in the history
Fix: panics if `AudioContextOptions#sinkId` is invalid
  • Loading branch information
orottier authored Jan 13, 2025
2 parents d6caae7 + e361058 commit 03df7e1
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/context/online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ impl AudioContext {
/// The `AudioContext` constructor will panic when an invalid `sinkId` is provided in the
/// `AudioContextOptions`. In a future version, a `try_new` constructor will be introduced that
/// never panics.
#[allow(clippy::needless_pass_by_value)]
#[must_use]
pub fn new(mut options: AudioContextOptions) -> Self {
// Log, but ignore invalid sinks
if !is_valid_sink_id(&options.sink_id) {
log::error!("NotFoundError: invalid sinkId {:?}", options.sink_id);
options.sink_id = String::from("");
}
pub fn new(options: AudioContextOptions) -> Self {
// https://webaudio.github.io/web-audio-api/#validating-sink-identifier
assert!(
is_valid_sink_id(&options.sink_id),
"NotFoundError - Invalid sinkId: {:?}",
options.sink_id
);

// Set up the audio output thread
let (control_thread_init, render_thread_init) = io::thread_init();
Expand Down Expand Up @@ -783,4 +783,14 @@ mod tests {
require_send_sync(context.resume());
require_send_sync(context.close());
}

#[test]
#[should_panic]
fn test_invalid_sink_id() {
let options = AudioContextOptions {
sink_id: "invalid".into(),
..AudioContextOptions::default()
};
let _ = AudioContext::new(options);
}
}

0 comments on commit 03df7e1

Please sign in to comment.