From 87b27bce8df162fb89455d309bcce4790919dd2d Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 7 Dec 2021 08:38:23 +0100 Subject: [PATCH] Prevent use-after-drop bug in set_viewport and set_clip_rect >`rect.into()` should be stored into a local variable as the call after it is using a pointer to it. Some builds might work because the stack value is not immediately reset. --- src/sdl2/render.rs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/sdl2/render.rs b/src/sdl2/render.rs index 6090dc2c..e88c90cf 100644 --- a/src/sdl2/render.rs +++ b/src/sdl2/render.rs @@ -1095,10 +1095,9 @@ impl Canvas { /// Sets the drawing area for rendering on the current target. #[doc(alias = "SDL_RenderSetViewport")] pub fn set_viewport>>(&mut self, rect: R) { - let ptr = match rect.into() { - Some(ref rect) => rect.raw(), - None => ptr::null(), - }; + let rect = rect.into(); + // as_ref is important because we need rect to live until the end of the FFI call, but map_or consumes an Option + let ptr = rect.as_ref().map_or(ptr::null(), |rect| rect.raw()); let ret = unsafe { sys::SDL_RenderSetViewport(self.context.raw, ptr) }; if ret != 0 { panic!("Could not set viewport: {}", get_error()) @@ -1119,15 +1118,10 @@ impl Canvas { /// If the rectangle is `None`, clipping will be disabled. #[doc(alias = "SDL_RenderSetClipRect")] pub fn set_clip_rect>>(&mut self, rect: R) { - let ret = unsafe { - sys::SDL_RenderSetClipRect( - self.context.raw, - match rect.into() { - Some(ref rect) => rect.raw(), - None => ptr::null(), - }, - ) - }; + let rect = rect.into(); + // as_ref is important because we need rect to live until the end of the FFI call, but map_or consumes an Option + let ptr = rect.as_ref().map_or(ptr::null(), |rect| rect.raw()); + let ret = unsafe { sys::SDL_RenderSetClipRect(self.context.raw, ptr) }; if ret != 0 { panic!("Could not set clip rect: {}", get_error()) }