From 239e5426e6275aeba271884a2047758b62dc10d1 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Tue, 2 Jan 2024 16:06:41 +0100 Subject: [PATCH] du: show error for nul names with --files0-from --- src/uu/du/src/du.rs | 13 +++++++++---- tests/by-util/test_du.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index e3f5a718654..3d590e1bf79 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -26,11 +26,11 @@ use std::sync::mpsc; use std::thread; use std::time::{Duration, UNIX_EPOCH}; use uucore::display::{print_verbatim, Quotable}; -use uucore::error::{FromIo, UError, UResult, USimpleError}; +use uucore::error::{set_exit_code, FromIo, UError, UResult, USimpleError}; use uucore::line_ending::LineEnding; use uucore::parse_glob; use uucore::parse_size::{parse_size_u64, ParseSizeError}; -use uucore::{format_usage, help_about, help_section, help_usage, show, show_warning}; +use uucore::{format_usage, help_about, help_section, help_usage, show, show_error, show_warning}; #[cfg(windows)] use windows_sys::Win32::Foundation::HANDLE; #[cfg(windows)] @@ -621,9 +621,14 @@ fn read_files_from(file_name: &str) -> Result, std::io::Error> { let mut paths = Vec::new(); - for line in reader.split(b'\0') { + for (i, line) in reader.split(b'\0').enumerate() { let path = line?; - if !path.is_empty() { + + if path.is_empty() { + let line_number = i + 1; + show_error!("{file_name}:{line_number}: invalid zero-length file name"); + set_exit_code(1); + } else { paths.push(PathBuf::from(String::from_utf8_lossy(&path).to_string())); } } diff --git a/tests/by-util/test_du.rs b/tests/by-util/test_du.rs index 5c2efdc9865..955759cb889 100644 --- a/tests/by-util/test_du.rs +++ b/tests/by-util/test_du.rs @@ -1010,6 +1010,24 @@ fn test_du_files0_from() { .stdout_contains("testdir"); } +#[test] +fn test_du_files0_from_with_invalid_zero_length_file_names() { + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; + + at.touch("testfile"); + + at.write("filelist", "\0testfile\0\0"); + + ts.ucmd() + .arg("--files0-from=filelist") + .fails() + .code_is(1) + .stdout_contains("testfile") + .stderr_contains("filelist:1: invalid zero-length file name") + .stderr_contains("filelist:3: invalid zero-length file name"); +} + #[test] fn test_du_files0_from_stdin() { let ts = TestScenario::new(util_name!()); @@ -1028,6 +1046,17 @@ fn test_du_files0_from_stdin() { .stdout_contains("testfile2"); } +#[test] +fn test_du_files0_from_stdin_with_invalid_zero_length_file_names() { + new_ucmd!() + .arg("--files0-from=-") + .pipe_in("\0\0") + .fails() + .code_is(1) + .stderr_contains("-:1: invalid zero-length file name") + .stderr_contains("-:2: invalid zero-length file name"); +} + #[test] fn test_du_files0_from_dir() { let ts = TestScenario::new(util_name!());