Skip to content

Commit

Permalink
Don't truncate the last character in ttyname (#832)
Browse files Browse the repository at this point in the history
CStr::len(), as used in backend::libc::termios::syscalls::ttyname, does
not include the trailing NUL.  But CString::from_vec_with_nul_unchecked
requires the NUL to be present.  Without it, it drops off the last
character from the string.  This led to it returning paths like
"/dev/pts/" instead of "/dev/pts/8".

Tested on FreeBSD and Linux x86_64
  • Loading branch information
asomers authored Sep 19, 2023
1 parent 4358096 commit 83bc861
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/termios/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ fn _ttyname(dirfd: BorrowedFd<'_>, mut buffer: Vec<u8>) -> io::Result<CString> {
buffer.reserve(buffer.capacity() + 1); // use `Vec` reallocation strategy to grow capacity exponentially
}
Ok(len) => {
// SAFETY: assume the backend returns the length of the string
// SAFETY: assume the backend returns the length of the string excluding the NUL.
unsafe {
buffer.set_len(len);
buffer.set_len(len + 1);
}

// SAFETY:
Expand Down

0 comments on commit 83bc861

Please sign in to comment.