diff --git a/src/windows.rs b/src/windows.rs index cc7f423..83e4b48 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -26,6 +26,7 @@ use self::winapi::um::winnt::*; use self::winapi::um::winver::*; use super::Uname; use std::borrow::Cow; +use std::convert::TryFrom; use std::ffi::{CStr, OsStr, OsString}; use std::io; use std::iter; @@ -105,7 +106,9 @@ impl PlatformInfo { let mut data: Vec = vec![0; size as usize]; unsafe { if GetComputerNameExW(ComputerNameDnsHostname, data.as_mut_ptr(), &mut size) != 0 { - Ok(String::from_utf16_lossy(&data)) + Ok(String::from_utf16_lossy( + &data[..usize::try_from(size).unwrap()], + )) } else { // XXX: should this error or just return localhost? Err(io::Error::last_os_error())