From fe74e7e6d0156563e42d0b234e8366c4ad93475b Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 17 May 2021 09:05:17 +0800 Subject: [PATCH] main: fix issue with non-null-terminated registry entries Signed-off-by: Sean Cross --- src/main.rs | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7948fca..be45de8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -92,26 +92,20 @@ fn get_lark_path() -> Option { }?; let v: std::io::Result = lark_cfg.get_raw_value("Lark Path"); let mut buf = v.unwrap(); - // Strip off NULL termination - buf.bytes.pop(); - buf.bytes.pop(); + + // Ensure the buffer is big enough. + if buf.bytes.len() <= 2 { + return None; + } + + // Sometimes it adds a NULL at the end. Strip off NULL termination. + if buf.bytes[buf.bytes.len()-2] == b'\0' { + buf.bytes.pop(); + buf.bytes.pop(); + } + let osstr = std::ffi::OsString::from_reg_value(&buf).unwrap(); Some(Path::new(&osstr).to_owned()) - // match v { - // Ok(v) => { - // print!("Values: "); - // use std::os::windows::ffi::OsStrExt; - // for (_idx, c) in v.encode_wide().enumerate() { - // print!(" {:04x}", c); - // } - // println!(); - - // let converted: PathBuf = v.into(); - // Some(converted.to_owned()) - // } - // Err(_) => None, - // } - // None } fn set_lark_path(new_path: &Path) -> std::io::Result<()> {