From f10faf21bc328fb5704cdb0361c5084e20e84d7a Mon Sep 17 00:00:00 2001 From: John Shin Date: Tue, 30 May 2023 11:26:39 -0700 Subject: [PATCH] head: interpret size as decimal --- src/uu/head/src/parse.rs | 8 +++++++- tests/by-util/test_head.rs | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/uu/head/src/parse.rs b/src/uu/head/src/parse.rs index b24b6591e..56c359a0c 100644 --- a/src/uu/head/src/parse.rs +++ b/src/uu/head/src/parse.rs @@ -114,7 +114,13 @@ pub fn parse_num(src: &str) -> Result<(u64, bool), ParseSizeError> { return Err(ParseSizeError::ParseFailure(src.to_string())); } - parse_size(size_string).map(|n| (n, all_but_last)) + // remove leading zeros so that size is interpreted as decimal, not octal + let trimmed_string = size_string.trim_start_matches('0'); + if trimmed_string.is_empty() { + Ok((0, all_but_last)) + } else { + parse_size(trimmed_string).map(|n| (n, all_but_last)) + } } #[cfg(test)] diff --git a/tests/by-util/test_head.rs b/tests/by-util/test_head.rs index 571bfb3a8..0e1eafc86 100644 --- a/tests/by-util/test_head.rs +++ b/tests/by-util/test_head.rs @@ -189,6 +189,15 @@ fn test_no_such_file_or_directory() { .stderr_contains("cannot open 'no_such_file.toml' for reading: No such file or directory"); } +#[test] +fn test_lines_leading_zeros() { + new_ucmd!() + .arg("--lines=010") + .pipe_in("\n\n\n\n\n\n\n\n\n\n\n\n") + .succeeds() + .stdout_is("\n\n\n\n\n\n\n\n\n\n"); +} + /// Test that each non-existent files gets its own error message printed. #[test] fn test_multiple_nonexistent_files() {