From 84ef934223531c673721c1003d0656d395f32012 Mon Sep 17 00:00:00 2001 From: AzureMarker Date: Mon, 10 Jan 2022 16:43:11 -0800 Subject: [PATCH] Add back slightly simpler Console::drop This is needed for soundness. Otherwise libctru will have a dangling pointer, and will write to it when printing. --- ctru-rs/src/console.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index e640109..21d2c66 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -50,3 +50,22 @@ impl Default for Console { Console::init(Screen::Top) } } + +impl Drop for Console { + fn drop(&mut self) { + unsafe { + // Get the current console by replacing it with the default. + let default_console = ctru_sys::consoleGetDefault(); + let current_console = ctru_sys::consoleSelect(default_console); + + if std::ptr::eq(current_console, &*self.context) { + // Console dropped while selected. We just replaced it with the + // default so there's nothing more to do. + } else { + // Console dropped while a different console was selected. Put back + // the console that was selected. + ctru_sys::consoleSelect(current_console); + } + } + } +}