From 9c896eb98b6aa77f4b4aeed84cd2a82a270e44b3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 25 Jul 2019 19:52:49 +0200 Subject: Add checks whether variables are valid unicode and propagate appropriate error Signed-off-by: Matthias Beyer --- lib/core/libimagrt/src/runtime.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/core/libimagrt/src/runtime.rs b/lib/core/libimagrt/src/runtime.rs index 43fa1eaf..ac3a233f 100644 --- a/lib/core/libimagrt/src/runtime.rs +++ b/lib/core/libimagrt/src/runtime.rs @@ -648,17 +648,26 @@ pub fn get_rtp_match<'a>(matches: &ArgMatches<'a>) -> Result { return Ok(p) } - if let Ok(home) = env::var("IMAG_RTP") { - return Ok(PathBuf::from(home)) + match env::var("IMAG_RTP").map(PathBuf::from) { + Ok(p) => return Ok(p), + Err(env::VarError::NotUnicode(_)) => { + return Err(err_msg("Environment variable 'IMAG_RTP' does not contain valid Unicode")) + }, + Err(env::VarError::NotPresent) => { /* passthrough */ } } env::var("HOME") .map(PathBuf::from) .map(|mut p| { p.push(".imag"); p }) - .map_err(|_| { - err_msg("You seem to be $HOME-less. Please get a $HOME before using this \ - software. We are sorry for you and hope you have some \ - accommodation anyways.") + .map_err(|e| match e { + env::VarError::NotUnicode(_) => { + err_msg("Environment variable 'HOME' does not contain valid Unicode") + }, + env::VarError::NotPresent => { + err_msg("You seem to be $HOME-less. Please get a $HOME before using this \ + software. We are sorry for you and hope you have some \ + accommodation anyways.") + } }) } -- cgit v1.2.3