Skip to content

Commit

Permalink
Merge pull request #5772 from cakebaker/du_files0_from_zero_length_fi…
Browse files Browse the repository at this point in the history
…le_name

du: show error for nul names with `--files0-from`
  • Loading branch information
sylvestre authored Jan 2, 2024
2 parents 9f257ad + 239e542 commit 8df064e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -621,9 +621,14 @@ fn read_files_from(file_name: &str) -> Result<Vec<PathBuf>, 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()));
}
}
Expand Down
29 changes: 29 additions & 0 deletions tests/by-util/test_du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!());
Expand All @@ -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!());
Expand Down

0 comments on commit 8df064e

Please sign in to comment.