Skip to content

Commit c8ffe26

Browse files
Merge #1745
1745: Change gethostname to use a buffer of MaybeUninit values r=asomers a=nathaniel-daniel Changing `gethostname` to accept a buffer of `MaybeUninit` bytes allows the user to avoid needlessly initializing a buffer. This is a breaking API change. Co-authored-by: Nathaniel Daniel <[email protected]>
2 parents caebe66 + 22c4ba8 commit c8ffe26

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
3232
(#[1713](https://github.com/nix-rust/nix/pull/1713))
3333
- `nix::poll::ppoll`: `sigmask` parameter is now optional.
3434
(#[1739](https://github.com/nix-rust/nix/pull/1739))
35+
- Changed `gethostname` to return an owned `OsString`.
36+
(#[1745](https://github.com/nix-rust/nix/pull/1745))
3537

3638
### Fixed
3739

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ call:
2424
// libc api (unsafe, requires handling return code/errno)
2525
pub unsafe extern fn gethostname(name: *mut c_char, len: size_t) -> c_int;
2626
27-
// nix api (returns a nix::Result<CStr>)
28-
pub fn gethostname<'a>(buffer: &'a mut [u8]) -> Result<&'a CStr>;
27+
// nix api (returns a nix::Result<OsString>)
28+
pub fn gethostname() -> Result<OsString>;
2929
```
3030

3131
## Supported Platforms

src/unistd.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -991,34 +991,34 @@ pub fn sethostname<S: AsRef<OsStr>>(name: S) -> Result<()> {
991991
Errno::result(res).map(drop)
992992
}
993993

994-
/// Get the host name and store it in the provided buffer, returning a pointer
995-
/// the `CStr` in that buffer on success (see
994+
/// Get the host name and store it in an internally allocated buffer, returning an
995+
/// `OsString` on success (see
996996
/// [gethostname(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/gethostname.html)).
997997
///
998998
/// This function call attempts to get the host name for the running system and
999-
/// store it in a provided buffer. The buffer will be populated with bytes up
1000-
/// to the length of the provided slice including a NUL terminating byte. If
1001-
/// the hostname is longer than the length provided, no error will be provided.
1002-
/// The posix specification does not specify whether implementations will
1003-
/// null-terminate in this case, but the nix implementation will ensure that the
1004-
/// buffer is null terminated in this case.
999+
/// store it in an internal buffer, returning it as an `OsString` if successful.
10051000
///
10061001
/// ```no_run
10071002
/// use nix::unistd;
10081003
///
1009-
/// let mut buf = [0u8; 64];
1010-
/// let hostname_cstr = unistd::gethostname(&mut buf).expect("Failed getting hostname");
1011-
/// let hostname = hostname_cstr.to_str().expect("Hostname wasn't valid UTF-8");
1004+
/// let hostname = unistd::gethostname().expect("Failed getting hostname");
1005+
/// let hostname = hostname.into_string().expect("Hostname wasn't valid UTF-8");
10121006
/// println!("Hostname: {}", hostname);
10131007
/// ```
1014-
pub fn gethostname(buffer: &mut [u8]) -> Result<&CStr> {
1008+
pub fn gethostname() -> Result<OsString> {
1009+
// The capacity is the max length of a hostname plus the NUL terminator.
1010+
let mut buffer: Vec<u8> = Vec::with_capacity(256);
10151011
let ptr = buffer.as_mut_ptr() as *mut c_char;
1016-
let len = buffer.len() as size_t;
1012+
let len = buffer.capacity() as size_t;
10171013

10181014
let res = unsafe { libc::gethostname(ptr, len) };
10191015
Errno::result(res).map(|_| {
1020-
buffer[len - 1] = 0; // ensure always null-terminated
1021-
unsafe { CStr::from_ptr(buffer.as_ptr() as *const c_char) }
1016+
unsafe {
1017+
buffer.as_mut_ptr().wrapping_add(len - 1).write(0); // ensure always null-terminated
1018+
let len = CStr::from_ptr(buffer.as_ptr() as *const c_char).len();
1019+
buffer.set_len(len);
1020+
}
1021+
OsString::from_vec(buffer)
10221022
})
10231023
}
10241024
}

0 commit comments

Comments
 (0)