From 3d356d47b3f6ada89b4946983d904f578f5263e2 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 5 Jan 2024 21:44:58 +0100 Subject: [PATCH] expand: avoid an infinite loop --- src/uu/expand/src/expand.rs | 11 +++++++++-- tests/by-util/test_expand.rs | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/uu/expand/src/expand.rs b/src/uu/expand/src/expand.rs index eb9766eb4..1efb36c65 100644 --- a/src/uu/expand/src/expand.rs +++ b/src/uu/expand/src/expand.rs @@ -11,11 +11,12 @@ use std::fmt; use std::fs::File; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; use std::num::IntErrorKind; +use std::path::Path; use std::str::from_utf8; use unicode_width::UnicodeWidthChar; use uucore::display::Quotable; -use uucore::error::{FromIo, UError, UResult}; -use uucore::{format_usage, help_about, help_usage}; +use uucore::error::{set_exit_code, FromIo, UError, UResult}; +use uucore::{format_usage, help_about, help_usage, show_error}; const ABOUT: &str = help_about!("expand.md"); const USAGE: &str = help_usage!("expand.md"); @@ -465,6 +466,12 @@ fn expand(options: &Options) -> UResult<()> { let mut buf = Vec::new(); for file in &options.files { + if Path::new(file).is_dir() { + show_error!("{}: Is a directory", file); + set_exit_code(1); + continue; + } + let mut fh = open(file)?; while match fh.read_until(b'\n', &mut buf) { diff --git a/tests/by-util/test_expand.rs b/tests/by-util/test_expand.rs index 1e26b3273..c420f5ad5 100644 --- a/tests/by-util/test_expand.rs +++ b/tests/by-util/test_expand.rs @@ -409,3 +409,11 @@ int main() { ", ); } + +#[test] +fn test_expand_directory() { + new_ucmd!() + .args(&["."]) + .fails() + .stderr_contains("expand: .: Is a directory"); +}