From f1eea15e00d794d83a42bc38594aa41889f9542d Mon Sep 17 00:00:00 2001 From: Cobrand Date: Tue, 2 May 2017 08:08:16 +0200 Subject: [PATCH] Fix segfault in VideoSystem::display_name Closes #650 --- src/sdl2/video.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index 2511f0f1..cc677d38 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -525,10 +525,18 @@ impl VideoSubsystem { } } - pub fn display_name(&self, display_index: i32) -> String { + /// Get the name of the display at the index `display_name`. + /// + /// Will return an error if the index is out of bounds or if SDL experienced a failure; inspect + /// the returned string for further info. + pub fn display_name(&self, display_index: i32) -> Result { unsafe { let display = ll::SDL_GetDisplayName(display_index as c_int); - CStr::from_ptr(display as *const _).to_str().unwrap().to_owned() + if display.is_null() { + Err(get_error()) + } else { + Ok(CStr::from_ptr(display as *const _).to_str().unwrap().to_owned()) + } } }