Correctly parse numbers starting with 0 as octal

This commit is contained in:
Joerg Jaspert 2022-06-12 12:55:33 +02:00
parent c513692bae
commit fd1e0e6dd7
2 changed files with 21 additions and 11 deletions

View file

@ -569,16 +569,20 @@ impl Config {
};
let width = match options.value_of(options::WIDTH) {
Some(x) => match x.parse::<u16>() {
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::<u16>() {
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") {

View file

@ -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::<Vec<_>>())
.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