Skip to content

Commit 090f4d5

Browse files
committed
Avoid panic in stty
The expect() calls cause stty to panic if we are not attached to a tty with ENOTTY. This in turn causes shells to print messages "Aborted (core dumped)", breaking tests that assume empty stderr [with normal stderr of the process being redirected]. Refactor the code to use .and_then() and .map_err() to map the resulting nix Errno into a boxed UIoError.
1 parent eb6bfce commit 090f4d5

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/uu/stty/src/stty.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -403,30 +403,29 @@ fn stty(opts: &Options) -> UResult<()> {
403403
}
404404
}
405405

406-
// TODO: Figure out the right error message for when tcgetattr fails
407-
let mut termios = tcgetattr(opts.file.as_fd()).expect("Could not get terminal attributes");
408-
409-
// iterate over valid_args, match on the arg type, do the matching apply function
410-
for arg in &valid_args {
411-
match arg {
412-
ArgOptions::Mapping(mapping) => apply_char_mapping(&mut termios, mapping),
413-
ArgOptions::Flags(flag) => apply_setting(&mut termios, flag),
414-
ArgOptions::Special(setting) => {
415-
apply_special_setting(&mut termios, setting, opts.file.as_raw_fd())?;
416-
}
417-
ArgOptions::Print(setting) => {
418-
print_special_setting(setting, opts.file.as_raw_fd())?;
406+
tcgetattr(opts.file.as_fd())
407+
.and_then(|mut termios| {
408+
// iterate over valid_args, match on the arg type, do the matching apply function
409+
for arg in &valid_args {
410+
match arg {
411+
ArgOptions::Mapping(mapping) => apply_char_mapping(&mut termios, mapping),
412+
ArgOptions::Flags(flag) => apply_setting(&mut termios, flag),
413+
ArgOptions::Special(setting) => {
414+
apply_special_setting(&mut termios, setting, opts.file.as_raw_fd())?;
415+
}
416+
ArgOptions::Print(setting) => {
417+
print_special_setting(setting, opts.file.as_raw_fd())?;
418+
}
419+
}
419420
}
420-
}
421-
}
422-
tcsetattr(opts.file.as_fd(), set_arg, &termios)
423-
.expect("Could not write terminal attributes");
421+
tcsetattr(opts.file.as_fd(), set_arg, &termios)
422+
})
423+
.map_err(|errno| errno.into())
424424
} else {
425-
// TODO: Figure out the right error message for when tcgetattr fails
426-
let termios = tcgetattr(opts.file.as_fd()).expect("Could not get terminal attributes");
427-
print_settings(&termios, opts).expect("TODO: make proper error here from nix error");
425+
tcgetattr(opts.file.as_fd())
426+
.and_then(|termios| print_settings(&termios, opts))
427+
.map_err(|errno| errno.into())
428428
}
429-
Ok(())
430429
}
431430

432431
fn missing_arg<T>(arg: &str) -> Result<T, Box<dyn UError>> {

0 commit comments

Comments
 (0)