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());