From fd1e0e6dd7ef0030f10dbebed73deb20681e54b1 Mon Sep 17 00:00:00 2001 From: Joerg Jaspert Date: Sun, 12 Jun 2022 12:55:33 +0200 Subject: [PATCH] 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 382528be0..30255b442 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 67c649f2c..739fe8b48 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