From 3db0623a721423a9680cef18b0bc2988204dc597 Mon Sep 17 00:00:00 2001 From: talwat <83217276+talwat@users.noreply.github.com> Date: Tue, 14 Jan 2025 11:10:33 +0100 Subject: [PATCH] fix: fix potential dangling pointer --- src/player.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/player.rs b/src/player.rs index f0317ed..3f86a1a 100644 --- a/src/player.rs +++ b/src/player.rs @@ -75,9 +75,8 @@ const TIMEOUT: Duration = Duration::from_secs(5); const BUFFER_SIZE: usize = 5; /// Main struct responsible for queuing up & playing tracks. -// TODO: Consider refactoring [Player] from being stored in an [Arc], -// TODO: so `Arc` into containing many smaller [Arc]s, being just -// TODO: `Player` as the type. +// TODO: Consider refactoring [Player] from being stored in an [Arc], into containing many smaller [Arc]s. +// TODO: In other words, this would change the type from `Arc` to just `Player`. // TODO: // TODO: This is conflicting, since then it'd clone ~10 smaller [Arc]s // TODO: every single time, which could be even worse than having an @@ -136,23 +135,25 @@ impl Player { // output to `/dev/null` so that it wont be shoved down our throats. // The mode which to redirect terminal output with. - let mode = CString::new("w")?.as_ptr(); + let mode = CString::new("w")?; // First redirect to /dev/null, which basically silences alsa. - let null = CString::new("/dev/null")?.as_ptr(); + let null = CString::new("/dev/null")?; + // SAFETY: Simple enough to be impossible to fail. Hopefully. unsafe { - freopen(null, mode, stderr); + freopen(null.as_ptr(), mode.as_ptr(), stderr); } // Make the OutputStream while stderr is still redirected to /dev/null. let (stream, handle) = OutputStream::try_default()?; // Redirect back to the current terminal, so that other output isn't silenced. - let tty = CString::new("/dev/tty")?.as_ptr(); + let tty = CString::new("/dev/tty")?; + // SAFETY: See the first call to `freopen`. unsafe { - freopen(tty, mode, stderr); + freopen(tty.as_ptr(), mode.as_ptr(), stderr); } Ok((stream, handle))