1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Merge pull request #4388 from howjmay/timeout-parse

fix: Fix panic in multi-byte characters
This commit is contained in:
Sylvestre Ledru 2023-02-22 21:23:06 +01:00 committed by GitHub
commit 380b3cff8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -51,7 +51,10 @@ pub fn from_str(string: &str) -> Result<Duration, String> {
if len == 0 {
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() {
's' => (slice, 1),
'm' => (slice, 60),
@ -112,6 +115,11 @@ mod tests {
assert!(from_str("123X").is_err());
}
#[test]
fn test_error_multi_bytes_characters() {
assert!(from_str("10€").is_err());
}
#[test]
fn test_error_invalid_magnitude() {
assert!(from_str("12abc3s").is_err());