File tree Expand file tree Collapse file tree 3 files changed +10
-5
lines changed Expand file tree Collapse file tree 3 files changed +10
-5
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
32
32
(#[ 1713] ( https://github.com/nix-rust/nix/pull/1713 ) )
33
33
- ` nix::poll::ppoll ` : ` sigmask ` parameter is now optional.
34
34
(#[ 1739] ( https://github.com/nix-rust/nix/pull/1739 ) )
35
+ - Changed ` gethostname ` to use a buffer of ` MaybeUninit ` values.
36
+ (#[ 1745] ( https://github.com/nix-rust/nix/pull/1745 ) )
35
37
36
38
### Fixed
37
39
Original file line number Diff line number Diff line change 25
25
pub unsafe extern fn gethostname(name: *mut c_char, len: size_t) -> c_int;
26
26
27
27
// nix api (returns a nix::Result<CStr>)
28
- pub fn gethostname<'a>(buffer: &'a mut [u8 ]) -> Result<&'a CStr>;
28
+ pub fn gethostname<'a>(buffer: &'a mut [mem::MaybeUninit<u8> ]) -> Result<&'a CStr>;
29
29
```
30
30
31
31
## Supported Platforms
Original file line number Diff line number Diff line change @@ -1005,20 +1005,23 @@ pub fn sethostname<S: AsRef<OsStr>>(name: S) -> Result<()> {
1005
1005
///
1006
1006
/// ```no_run
1007
1007
/// use nix::unistd;
1008
+ /// use std::mem;
1008
1009
///
1009
- /// let mut buf = [0u8 ; 64];
1010
+ /// let mut buf = [mem::MaybeUninit::uninit() ; 64];
1010
1011
/// let hostname_cstr = unistd::gethostname(&mut buf).expect("Failed getting hostname");
1011
1012
/// let hostname = hostname_cstr.to_str().expect("Hostname wasn't valid UTF-8");
1012
1013
/// println!("Hostname: {}", hostname);
1013
1014
/// ```
1014
- pub fn gethostname( buffer: & mut [ u8 ] ) -> Result <& CStr > {
1015
+ pub fn gethostname( buffer: & mut [ mem :: MaybeUninit < u8 > ] ) -> Result <& CStr > {
1015
1016
let ptr = buffer. as_mut_ptr( ) as * mut c_char;
1016
1017
let len = buffer. len( ) as size_t;
1017
1018
1018
1019
let res = unsafe { libc:: gethostname( ptr, len) } ;
1019
1020
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) }
1021
+ unsafe {
1022
+ buffer[ len - 1 ] . as_mut_ptr( ) . write( 0 ) ; // ensure always null-terminated
1023
+ CStr :: from_ptr( buffer. as_ptr( ) as * const c_char)
1024
+ }
1022
1025
} )
1023
1026
}
1024
1027
}
You can’t perform that action at this time.
0 commit comments