mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
Merge pull request #4388 from howjmay/timeout-parse
fix: Fix panic in multi-byte characters
This commit is contained in:
commit
380b3cff8c
1 changed files with 9 additions and 1 deletions
|
@ -51,7 +51,10 @@ pub fn from_str(string: &str) -> Result<Duration, String> {
|
||||||
if len == 0 {
|
if len == 0 {
|
||||||
return Err("empty string".to_owned());
|
return Err("empty string".to_owned());
|
||||||
}
|
}
|
||||||
let slice = &string[..len - 1];
|
let slice = match string.get(..len - 1) {
|
||||||
|
Some(s) => s,
|
||||||
|
None => return Err(format!("invalid time interval {}", string.quote())),
|
||||||
|
};
|
||||||
let (numstr, times) = match string.chars().next_back().unwrap() {
|
let (numstr, times) = match string.chars().next_back().unwrap() {
|
||||||
's' => (slice, 1),
|
's' => (slice, 1),
|
||||||
'm' => (slice, 60),
|
'm' => (slice, 60),
|
||||||
|
@ -112,6 +115,11 @@ mod tests {
|
||||||
assert!(from_str("123X").is_err());
|
assert!(from_str("123X").is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_error_multi_bytes_characters() {
|
||||||
|
assert!(from_str("10€").is_err());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_error_invalid_magnitude() {
|
fn test_error_invalid_magnitude() {
|
||||||
assert!(from_str("12abc3s").is_err());
|
assert!(from_str("12abc3s").is_err());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue