Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#4886) more: panics if file is not readable #4888

Merged
merged 6 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/uu/more/src/more.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,17 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if length > 1 {
buff.push_str(&MULTI_FILE_TOP_PROMPT.replace("{}", file.to_str().unwrap()));
}
let mut reader = BufReader::new(File::open(file).unwrap());
let opened_file = match File::open(file) {
Err(why) => {
terminal::disable_raw_mode().unwrap();
return Err(USimpleError::new(
1,
format!("cannot open {}: {}", file.quote(), why.kind()),
));
}
Ok(opened_file) => opened_file,
};
let mut reader = BufReader::new(opened_file);
reader.read_to_string(&mut buff).unwrap();
more(&buff, &mut stdout, next_file.copied(), &options)?;
buff.clear();
Expand Down
18 changes: 18 additions & 0 deletions tests/by-util/test_more.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use crate::common::util::TestScenario;
use is_terminal::IsTerminal;

//Both following includes are only needed inside the test_more_invalid_file_perms()
#[cfg(target_family = "unix")]
use std::fs::{set_permissions, Permissions};
#[cfg(target_family = "unix")]
use std::os::unix::fs::PermissionsExt;
Ludmuterol marked this conversation as resolved.
Show resolved Hide resolved

#[test]
fn test_more_no_arg() {
// Reading from stdin is now supported, so this must succeed
Expand Down Expand Up @@ -32,3 +38,15 @@ fn test_more_dir_arg() {
.usage_error("'.' is a directory.");
}
}

#[test]
#[cfg(target_family = "unix")]
fn test_more_invalid_file_perms() {
if std::io::stdout().is_terminal() {
let (at, mut ucmd) = at_and_ucmd!();
let permissions = Permissions::from_mode(0o244);
at.make_file("invalid-perms.txt");
set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap();
ucmd.arg("invalid-perms.txt").fails();
Ludmuterol marked this conversation as resolved.
Show resolved Hide resolved
}
}