From ed20f357dc64afd62b8e2464e9984d9db31b12e0 Mon Sep 17 00:00:00 2001 From: Johannesd3 <51954457+Johannesd3@users.noreply.github.com> Date: Fri, 29 Jan 2021 03:01:38 +0100 Subject: [PATCH] Fix playback in pulseaudio backend (#577) * Fix playback in pulseaudio backend * Add comment regarding safety --- playback/src/audio_backend/pulseaudio.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/playback/src/audio_backend/pulseaudio.rs b/playback/src/audio_backend/pulseaudio.rs index e0b9ad9b..6c8d7211 100644 --- a/playback/src/audio_backend/pulseaudio.rs +++ b/playback/src/audio_backend/pulseaudio.rs @@ -67,7 +67,11 @@ impl Sink for PulseAudioSink { fn write(&mut self, data: &[i16]) -> io::Result<()> { if let Some(s) = &self.s { - let d: &[u8] = unsafe { std::mem::transmute(data) }; + // SAFETY: An i16 consists of two bytes, so that the given slice can be interpreted + // as a byte array of double length. Each byte pointer is validly aligned, and so + // is the newly created slice. + let d: &[u8] = + unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * 2) }; match s.write(d) { Ok(_) => Ok(()),