From 6b1d35eb4d1d7b17e49c27c66b0b536aaa9ba6fa Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sat, 14 Jan 2023 23:58:30 -0600 Subject: [PATCH] fix nodename string conversion (fixes #32) --- src/windows.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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())