From 83bc861ba5bff25836d41d0b866acc5e7fc7915c Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 18 Sep 2023 21:55:55 -0600 Subject: [PATCH] Don't truncate the last character in ttyname (#832) 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 --- src/termios/tty.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/termios/tty.rs b/src/termios/tty.rs index e28a959c1..f66210d08 100644 --- a/src/termios/tty.rs +++ b/src/termios/tty.rs @@ -54,9 +54,9 @@ fn _ttyname(dirfd: BorrowedFd<'_>, mut buffer: Vec) -> io::Result { 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: