Skip to content

Commit

Permalink
fixup! Add safety asserts to windows code
Browse files Browse the repository at this point in the history
  • Loading branch information
swsnr committed Oct 27, 2022
1 parent 4537777 commit abcacd7
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/lib.rs
Expand Up @@ -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(),
Expand All @@ -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());
Expand Down

0 comments on commit abcacd7

Please sign in to comment.