Skip to content

Commit 03df7e1

Browse files
authored
Merge pull request #545 from b-ma/fix/invalid_sink_id
Fix: panics if `AudioContextOptions#sinkId` is invalid
2 parents d6caae7 + e361058 commit 03df7e1

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/context/online.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ impl AudioContext {
178178
/// The `AudioContext` constructor will panic when an invalid `sinkId` is provided in the
179179
/// `AudioContextOptions`. In a future version, a `try_new` constructor will be introduced that
180180
/// never panics.
181-
#[allow(clippy::needless_pass_by_value)]
182181
#[must_use]
183-
pub fn new(mut options: AudioContextOptions) -> Self {
184-
// Log, but ignore invalid sinks
185-
if !is_valid_sink_id(&options.sink_id) {
186-
log::error!("NotFoundError: invalid sinkId {:?}", options.sink_id);
187-
options.sink_id = String::from("");
188-
}
182+
pub fn new(options: AudioContextOptions) -> Self {
183+
// https://webaudio.github.io/web-audio-api/#validating-sink-identifier
184+
assert!(
185+
is_valid_sink_id(&options.sink_id),
186+
"NotFoundError - Invalid sinkId: {:?}",
187+
options.sink_id
188+
);
189189

190190
// Set up the audio output thread
191191
let (control_thread_init, render_thread_init) = io::thread_init();
@@ -783,4 +783,14 @@ mod tests {
783783
require_send_sync(context.resume());
784784
require_send_sync(context.close());
785785
}
786+
787+
#[test]
788+
#[should_panic]
789+
fn test_invalid_sink_id() {
790+
let options = AudioContextOptions {
791+
sink_id: "invalid".into(),
792+
..AudioContextOptions::default()
793+
};
794+
let _ = AudioContext::new(options);
795+
}
786796
}

0 commit comments

Comments
 (0)