diff options
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | src/pty.rs | 21 |
2 files changed, 13 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index fcc8d4c2..7b1fd3bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,9 @@ This project adheres to [Semantic Versioning](https://semver.org/). (#[1440](https://github.com/nix-rust/nix/pull/1440)) - Minimum supported Rust version is now 1.41.0. ([#1440](https://github.com/nix-rust/nix/pull/1440)) +- `ptsname_r` now returns a lossily-converted string in the event of bad UTF, + just like `ptsname`. + ([#1446](https://github.com/nix-rust/nix/pull/1446)) - Errno aliases are now associated consts on `Errno`, instead of consts in the `errno` module. (#[1452](https://github.com/nix-rust/nix/pull/1452)) @@ -190,18 +190,17 @@ pub unsafe fn ptsname(fd: &PtyMaster) -> Result<String> { #[cfg(any(target_os = "android", target_os = "linux"))] #[inline] pub fn ptsname_r(fd: &PtyMaster) -> Result<String> { - let mut name_buf = vec![0u8; 64]; - let name_buf_ptr = name_buf.as_mut_ptr() as *mut libc::c_char; - if unsafe { libc::ptsname_r(fd.as_raw_fd(), name_buf_ptr, name_buf.capacity()) } != 0 { - return Err(Error::last()); - } - - // Find the first null-character terminating this string. This is guaranteed to succeed if the - // return value of `libc::ptsname_r` is 0. - let null_index = name_buf.iter().position(|c| *c == b'\0').unwrap(); - name_buf.truncate(null_index); + let mut name_buf = Vec::<libc::c_char>::with_capacity(64); + let name_buf_ptr = name_buf.as_mut_ptr(); + let cname = unsafe { + let cap = name_buf.capacity(); + if libc::ptsname_r(fd.as_raw_fd(), name_buf_ptr, cap) != 0 { + return Err(Error::last()); + } + CStr::from_ptr(name_buf.as_ptr()) + }; - let name = String::from_utf8(name_buf)?; + let name = cname.to_string_lossy().into_owned(); Ok(name) } |