From abcacd78e841682dc239d1cc78405f355a7edaf0 Mon Sep 17 00:00:00 2001 From: Sebastian Wiesner Date: Thu, 27 Oct 2022 13:37:13 +0200 Subject: [PATCH] fixup! Add safety asserts to windows code --- src/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 19bf9dd..385a4c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -98,7 +98,8 @@ fn gethostname_impl() -> OsString { unsafe { // This call always fails with ERROR_MORE_DATA, because we pass NULL to - // get the required buffer size. + // get the required buffer size. GetComputerNameExW then fills buffer_size with the size + // of the host name string plus a trailing zero byte. GetComputerNameExW( ComputerNamePhysicalDnsHostname, PWSTR::null(), @@ -123,8 +124,9 @@ fn gethostname_impl() -> OsString { ) } assert!( - buffer_size as usize == buffer.len(), - "GetComputerNameExW unexpectedly changed the buffer size" + // GetComputerNameExW returns the size _without_ the trailing zero byte on the second call + buffer_size as usize == buffer.len() - 1, + "GetComputerNameExW changed the buffer size unexpectedly" ); let end = buffer.iter().position(|&b| b == 0).unwrap_or(buffer.len());