Skip to content

Commit

Permalink
Merge pull request #3613 from Ganneff/lswidthoctal
Browse files Browse the repository at this point in the history
Forbid octal numbers for width parameter
  • Loading branch information
sylvestre authored Jun 13, 2022
2 parents b1fbbb6 + fd1e0e6 commit 76d6a4a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,20 @@ impl Config {
};

let width = match options.value_of(options::WIDTH) {
Some(x) => match x.parse::<u16>() {
Ok(u) => u,
Err(_) => return Err(LsError::InvalidLineWidth(x.into()).into()),
},
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::<u16>() {
Ok(u) => u,
Err(_) => return Err(LsError::InvalidLineWidth(x.into()).into()),
}
}
}
None => match termsize::get() {
Some(size) => size.cols,
None => match std::env::var_os("COLUMNS") {
Expand Down
15 changes: 15 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,21 @@ fn test_ls_width() {
.stdout_only("test-width-1 test-width-2 test-width-3 test-width-4\n");
}

for option in [
"-w 062",
"-w=062",
"--width=062",
"--width 062",
"--wid=062",
] {
scene
.ucmd()
.args(&option.split(' ').collect::<Vec<_>>())
.arg("-C")
.succeeds()
.stdout_only("test-width-1 test-width-3\ntest-width-2 test-width-4\n");
}

scene
.ucmd()
.arg("-w=bad")
Expand Down

0 comments on commit 76d6a4a

Please sign in to comment.