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

du: show error for nul names with --files0-from #5772

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading