From aec63c06fda5ffba3b870ff898d904b5cfedcbed Mon Sep 17 00:00:00 2001 From: Joerg Jaspert Date: Fri, 10 Jun 2022 23:42:16 +0200 Subject: [PATCH 1/3] Forbid octal numbers for width parameter --- src/uu/ls/src/ls.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 2ebd2fb973d..382528be009 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -570,7 +570,13 @@ impl Config { let width = match options.value_of(options::WIDTH) { Some(x) => match x.parse::() { - Ok(u) => u, + Ok(u) => { + if u != 0 && x.starts_with('0') { + return Err(LsError::InvalidLineWidth(x.into()).into()); + } else { + u + } + } Err(_) => return Err(LsError::InvalidLineWidth(x.into()).into()), }, None => match termsize::get() { From c513692bae59d9349a9cb0de7354a6b21c425fec Mon Sep 17 00:00:00 2001 From: Joerg Jaspert Date: Sat, 11 Jun 2022 22:43:46 +0200 Subject: [PATCH 2/3] Test for (against) octal-looking width --- tests/by-util/test_ls.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index db2c3d445f2..67c649f2cd9 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -650,6 +650,15 @@ fn test_ls_width() { .stdout_only("test-width-1 test-width-2 test-width-3 test-width-4\n"); } + for option in ["-w 06", "-w=06", "--width=06", "--width 06", "--wid=06"] { + scene + .ucmd() + .args(&option.split(' ').collect::>()) + .arg("-C") + .fails() + .stderr_contains("invalid line width"); + } + scene .ucmd() .arg("-w=bad") From fd1e0e6dd7ef0030f10dbebed73deb20681e54b1 Mon Sep 17 00:00:00 2001 From: Joerg Jaspert Date: Sun, 12 Jun 2022 12:55:33 +0200 Subject: [PATCH 3/3] Correctly parse numbers starting with 0 as octal --- src/uu/ls/src/ls.rs | 20 ++++++++++++-------- tests/by-util/test_ls.rs | 12 +++++++++--- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 382528be009..30255b4422f 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -569,16 +569,20 @@ impl Config { }; let width = match options.value_of(options::WIDTH) { - Some(x) => match x.parse::() { - Ok(u) => { - if u != 0 && x.starts_with('0') { - return Err(LsError::InvalidLineWidth(x.into()).into()); - } else { - u + Some(x) => { + if x.starts_with('0') && x.len() > 1 { + // Read number as octal + match u16::from_str_radix(x, 8) { + Ok(v) => v, + Err(_) => return Err(LsError::InvalidLineWidth(x.into()).into()), + } + } else { + match x.parse::() { + Ok(u) => u, + Err(_) => return Err(LsError::InvalidLineWidth(x.into()).into()), } } - Err(_) => return Err(LsError::InvalidLineWidth(x.into()).into()), - }, + } None => match termsize::get() { Some(size) => size.cols, None => match std::env::var_os("COLUMNS") { diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 67c649f2cd9..739fe8b4893 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -650,13 +650,19 @@ fn test_ls_width() { .stdout_only("test-width-1 test-width-2 test-width-3 test-width-4\n"); } - for option in ["-w 06", "-w=06", "--width=06", "--width 06", "--wid=06"] { + for option in [ + "-w 062", + "-w=062", + "--width=062", + "--width 062", + "--wid=062", + ] { scene .ucmd() .args(&option.split(' ').collect::>()) .arg("-C") - .fails() - .stderr_contains("invalid line width"); + .succeeds() + .stdout_only("test-width-1 test-width-3\ntest-width-2 test-width-4\n"); } scene