summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md2
-rw-r--r--README.md2
-rw-r--r--src/unistd.rs11
3 files changed, 10 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b7c3df5f..6edfba90 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,6 +32,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
(#[1713](https://github.com/nix-rust/nix/pull/1713))
- `nix::poll::ppoll`: `sigmask` parameter is now optional.
(#[1739](https://github.com/nix-rust/nix/pull/1739))
+- Changed `gethostname` to use a buffer of `MaybeUninit` values.
+ (#[1745](https://github.com/nix-rust/nix/pull/1745))
### Fixed
diff --git a/README.md b/README.md
index 44e620cc..04f9bd61 100644
--- a/README.md
+++ b/README.md
@@ -25,7 +25,7 @@ call:
pub unsafe extern fn gethostname(name: *mut c_char, len: size_t) -> c_int;
// nix api (returns a nix::Result<CStr>)
-pub fn gethostname<'a>(buffer: &'a mut [u8]) -> Result<&'a CStr>;
+pub fn gethostname<'a>(buffer: &'a mut [mem::MaybeUninit<u8>]) -> Result<&'a CStr>;
```
## Supported Platforms
diff --git a/src/unistd.rs b/src/unistd.rs
index 86eec627..6a7f0c37 100644
--- a/src/unistd.rs
+++ b/src/unistd.rs
@@ -1005,20 +1005,23 @@ pub fn sethostname<S: AsRef<OsStr>>(name: S) -> Result<()> {
///
/// ```no_run
/// use nix::unistd;
+/// use std::mem;
///
-/// let mut buf = [0u8; 64];
+/// let mut buf = [mem::MaybeUninit::uninit(); 64];
/// let hostname_cstr = unistd::gethostname(&mut buf).expect("Failed getting hostname");
/// let hostname = hostname_cstr.to_str().expect("Hostname wasn't valid UTF-8");
/// println!("Hostname: {}", hostname);
/// ```
-pub fn gethostname(buffer: &mut [u8]) -> Result<&CStr> {
+pub fn gethostname(buffer: &mut [mem::MaybeUninit<u8>]) -> Result<&CStr> {
let ptr = buffer.as_mut_ptr() as *mut c_char;
let len = buffer.len() as size_t;
let res = unsafe { libc::gethostname(ptr, len) };
Errno::result(res).map(|_| {
- buffer[len - 1] = 0; // ensure always null-terminated
- unsafe { CStr::from_ptr(buffer.as_ptr() as *const c_char) }
+ unsafe {
+ buffer[len - 1].as_mut_ptr().write(0); // ensure always null-terminated
+ CStr::from_ptr(buffer.as_ptr() as *const c_char)
+ }
})
}
}