diff options
author | Mikayla Maki <mikayla.c.maki@gmail.com> | 2022-07-20 15:30:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-20 22:30:55 +0000 |
commit | ed4614d0bd2a828a9d89347ad675d4564a3af754 (patch) | |
tree | 1e07e21bf7d4e6039e161ef1263f41853f2870b9 | |
parent | 48454c004700e359c3c496871643913fb20de84f (diff) | |
download | alacritty-ed4614d0bd2a828a9d89347ad675d4564a3af754.tar.gz alacritty-ed4614d0bd2a828a9d89347ad675d4564a3af754.zip |
Remove panics from UNIX PTY initialization
Co-authored-by: Mikayla Maki <mikayla.c.maki@icloud.com>
-rw-r--r-- | alacritty_terminal/src/tty/unix.rs | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/alacritty_terminal/src/tty/unix.rs b/alacritty_terminal/src/tty/unix.rs index d1e5bdb5..078621df 100644 --- a/alacritty_terminal/src/tty/unix.rs +++ b/alacritty_terminal/src/tty/unix.rs @@ -33,14 +33,14 @@ macro_rules! die { } /// Get raw fds for master/slave ends of a new PTY. -fn make_pty(size: winsize) -> (RawFd, RawFd) { +fn make_pty(size: winsize) -> Result<(RawFd, RawFd)> { let mut window_size = size; window_size.ws_xpixel = 0; window_size.ws_ypixel = 0; - let ends = openpty(Some(&window_size), None).expect("openpty failed"); + let ends = openpty(Some(&window_size), None)?; - (ends.master, ends.slave) + Ok((ends.master, ends.slave)) } /// Really only needed on BSD, but should be fine elsewhere. @@ -71,7 +71,7 @@ struct Passwd<'a> { /// # Unsafety /// /// If `buf` is changed while `Passwd` is alive, bad thing will almost certainly happen. -fn get_pw_entry(buf: &mut [i8; 1024]) -> Passwd<'_> { +fn get_pw_entry(buf: &mut [i8; 1024]) -> Result<Passwd<'_>> { // Create zeroed passwd struct. let mut entry: MaybeUninit<libc::passwd> = MaybeUninit::uninit(); @@ -85,22 +85,22 @@ fn get_pw_entry(buf: &mut [i8; 1024]) -> Passwd<'_> { let entry = unsafe { entry.assume_init() }; if status < 0 { - die!("getpwuid_r failed"); + return Err(Error::new(ErrorKind::Other, "getpwuid_r failed")); } if res.is_null() { - die!("pw not found"); + return Err(Error::new(ErrorKind::Other, "pw not found")); } // Sanity check. assert_eq!(entry.pw_uid, uid); // Build a borrowed Passwd struct. - Passwd { + Ok(Passwd { name: unsafe { CStr::from_ptr(entry.pw_name).to_str().unwrap() }, dir: unsafe { CStr::from_ptr(entry.pw_dir).to_str().unwrap() }, shell: unsafe { CStr::from_ptr(entry.pw_shell).to_str().unwrap() }, - } + }) } pub struct Pty { @@ -136,7 +136,7 @@ fn default_shell(pw: &Passwd<'_>) -> Program { /// Create a new TTY and return a handle to interact with it. pub fn new(config: &PtyConfig, window_size: WindowSize, window_id: Option<usize>) -> Result<Pty> { - let (master, slave) = make_pty(window_size.to_winsize()); + let (master, slave) = make_pty(window_size.to_winsize())?; #[cfg(any(target_os = "linux", target_os = "macos"))] if let Ok(mut termios) = termios::tcgetattr(master) { @@ -146,7 +146,7 @@ pub fn new(config: &PtyConfig, window_size: WindowSize, window_id: Option<usize> } let mut buf = [0; 1024]; - let pw = get_pw_entry(&mut buf); + let pw = get_pw_entry(&mut buf)?; let shell = match config.shell.as_ref() { Some(shell) => Cow::Borrowed(shell), @@ -383,5 +383,5 @@ unsafe fn set_nonblocking(fd: c_int) { #[test] fn test_get_pw_entry() { let mut buf: [i8; 1024] = [0; 1024]; - let _pw = get_pw_entry(&mut buf); + let _pw = get_pw_entry(&mut buf).unwrap(); } |