summaryrefslogtreecommitdiff
path: root/src/unistd.rs
diff options
context:
space:
mode:
authorNathaniel Daniel <nathaniel.daniel12@gmail.com>2022-06-14 19:38:17 -0700
committerNathaniel Daniel <nathaniel.daniel12@gmail.com>2022-06-27 19:48:14 -0700
commit356ecce5cd196afe533d881d888856fd41fe624e (patch)
tree093b52eb3e75ed5cf6297a7cd356ec1a109c87dd /src/unistd.rs
parent0922fd99e7c8cbe7334754d712e8450c29d3d8e7 (diff)
downloadnix-356ecce5cd196afe533d881d888856fd41fe624e.zip
Change gethostname to use a buffer of MaybeUninit values
Diffstat (limited to 'src/unistd.rs')
-rw-r--r--src/unistd.rs11
1 files changed, 7 insertions, 4 deletions
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)
+ }
})
}
}