bug(ls): when --tabsize is a negative number ls successeds instead of rejecting it#13278
Conversation
for more information, see https://pre-commit.ci
|
some jobs are still failing |
|
@sylvestre yeah, that because of clippy, im fixing them rn |
Merging this PR will not alter performance
Comparing Footnotes
|
|
@sylvestre ready for review :) |
|
GNU testsuite comparison: |
| } else { | ||
| return Err(Box::new(LsError::InvalidTabSize(size_str.clone()))); | ||
| } | ||
| } |
There was a problem hiding this comment.
it should be moved in a function
and it should look more like (ie without the Ok())
fn parse_tab_size(size_str: &str) -> Result<usize, Box<LsError>> {
size_str
.parse::<usize>()
.ok()
.or_else(|| {
size_str
.strip_prefix("0x")
.or_else(|| size_str.strip_prefix("0X"))
.and_then(|hex| usize::from_str_radix(hex, 16).ok())
})
.ok_or_else(|| Box::new(LsError::InvalidTabSize(size_str.to_string())))
}
There was a problem hiding this comment.
Alright, I'm going to use that function, thanks!
| } | ||
|
|
||
| impl Config { | ||
| fn parse_tab_size(size_str: &str) -> Result<usize, Box<LsError>> { |
There was a problem hiding this comment.
oops, i'll move it under parse_width, sorry
| match Self::parse_tab_size(size_str) { | ||
| Ok(n) => Some(n), | ||
| Err(e) => return Err(e), | ||
| } | ||
| } else { | ||
| options | ||
| .get_one::<String>(options::format::TAB_SIZE) | ||
| .and_then(|size| size.parse::<usize>().ok()) | ||
| .or_else(|| std::env::var("TABSIZE").ok().and_then(|s| s.parse().ok())) | ||
| } | ||
| .unwrap_or(SPACES_IN_TAB); | ||
| None | ||
| }; |
There was a problem hiding this comment.
something like ?
| match Self::parse_tab_size(size_str) { | |
| Ok(n) => Some(n), | |
| Err(e) => return Err(e), | |
| } | |
| } else { | |
| options | |
| .get_one::<String>(options::format::TAB_SIZE) | |
| .and_then(|size| size.parse::<usize>().ok()) | |
| .or_else(|| std::env::var("TABSIZE").ok().and_then(|s| s.parse().ok())) | |
| } | |
| .unwrap_or(SPACES_IN_TAB); | |
| None | |
| }; | |
| Some(Self::parse_tab_size(size_str)? | |
| }; |
There was a problem hiding this comment.
We can't directly propagate LsError, can we?
|
|
||
| scene.ucmd().args(&["-T", "3"]).succeeds(); | ||
| scene.ucmd().args(&["--tabsize", "0"]).succeeds(); | ||
| scene.ucmd().arg("--tabsize=-3").fails(); |
There was a problem hiding this comment.
verify the error message
and also add a string and a decimal for --tabsize=
* tests/ls/ls-misc.pl (tabsize-negative, tabsize-non-numeric): New tests verifying that a negative or non-numeric --tabsize argument is rejected with "invalid tab size" and exit status 2. uutils/coreutils#13278
| let tab_size = if needs_color { | ||
| Some(0) | ||
| } else if let Some(size_str) = options.get_one::<String>(options::format::TAB_SIZE) { | ||
| match parse_tab_size(size_str) { |
There was a problem hiding this comment.
this needs to be improve too
There was a problem hiding this comment.
we can use match to check if its none or not, but I think that's the most readable way, should I do it with match?
There was a problem hiding this comment.
} else if let Some(size_str) = options.get_one::<String>(options::format::TAB_SIZE) {
Some(parse_tab_size(size_str)?)
} else {
is the same
There was a problem hiding this comment.
yeah, but that can't be done, as parse_tab_size returns an LsError and it cannot be propagated to from()
* tests/ls/ls-misc.pl: Add tests, generated by a loop over invalid values, verifying that a negative (-9), non-numeric (zz), or fractional (0.5) --tabsize argument is rejected with "invalid tab size" and exit status 2. uutils/coreutils#13278
|
#13283 as follow up |
|
Why do we have our own parser at here instead of clap's validation? |
this PR resolves #12835
Whenever the value from --tabsize can't be parsed, it'll now throw an error, which message is identical to GNU's one. error code is 2, like in GNU, hexadecimal values are accepted