mirror of
https://github.com/uutils/coreutils
synced 2024-12-13 23:02:38 +00:00
Merge pull request #5074 from Skryptonyte/ls_clampwidth
ls: Limit value of --width to maximum value if overflowing
This commit is contained in:
commit
bd4ecb6cfb
2 changed files with 33 additions and 14 deletions
|
@ -17,6 +17,8 @@ use lscolors::LsColors;
|
||||||
use number_prefix::NumberPrefix;
|
use number_prefix::NumberPrefix;
|
||||||
use once_cell::unsync::OnceCell;
|
use once_cell::unsync::OnceCell;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
use std::num::IntErrorKind;
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
use std::os::windows::fs::MetadataExt;
|
use std::os::windows::fs::MetadataExt;
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -657,6 +659,19 @@ fn extract_indicator_style(options: &clap::ArgMatches) -> IndicatorStyle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_width(s: &str) -> Result<u16, LsError> {
|
||||||
|
let radix = match s.starts_with('0') && s.len() > 1 {
|
||||||
|
true => 8,
|
||||||
|
false => 10,
|
||||||
|
};
|
||||||
|
match u16::from_str_radix(s, radix) {
|
||||||
|
Ok(x) => Ok(x),
|
||||||
|
Err(e) => match e.kind() {
|
||||||
|
IntErrorKind::PosOverflow => Ok(u16::MAX),
|
||||||
|
_ => Err(LsError::InvalidLineWidth(s.into())),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
impl Config {
|
impl Config {
|
||||||
#[allow(clippy::cognitive_complexity)]
|
#[allow(clippy::cognitive_complexity)]
|
||||||
pub fn from(options: &clap::ArgMatches) -> UResult<Self> {
|
pub fn from(options: &clap::ArgMatches) -> UResult<Self> {
|
||||||
|
@ -795,20 +810,7 @@ impl Config {
|
||||||
};
|
};
|
||||||
|
|
||||||
let width = match options.get_one::<String>(options::WIDTH) {
|
let width = match options.get_one::<String>(options::WIDTH) {
|
||||||
Some(x) => {
|
Some(x) => parse_width(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 terminal_size::terminal_size() {
|
None => match terminal_size::terminal_size() {
|
||||||
Some((width, _)) => width.0,
|
Some((width, _)) => width.0,
|
||||||
None => match std::env::var_os("COLUMNS") {
|
None => match std::env::var_os("COLUMNS") {
|
||||||
|
|
|
@ -670,6 +670,23 @@ fn test_ls_width() {
|
||||||
.stdout_only("test-width-1 test-width-3\ntest-width-2 test-width-4\n");
|
.stdout_only("test-width-1 test-width-3\ntest-width-2 test-width-4\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for option in [
|
||||||
|
"-w 100000000000000",
|
||||||
|
"-w=100000000000000",
|
||||||
|
"--width=100000000000000",
|
||||||
|
"--width 100000000000000",
|
||||||
|
"-w 07777777777777777777",
|
||||||
|
"-w=07777777777777777777",
|
||||||
|
"--width=07777777777777777777",
|
||||||
|
"--width 07777777777777777777",
|
||||||
|
] {
|
||||||
|
scene
|
||||||
|
.ucmd()
|
||||||
|
.args(&option.split(' ').collect::<Vec<_>>())
|
||||||
|
.arg("-C")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("test-width-1 test-width-2 test-width-3 test-width-4\n");
|
||||||
|
}
|
||||||
scene
|
scene
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.arg("-w=bad")
|
.arg("-w=bad")
|
||||||
|
|
Loading…
Reference in a new issue