From 7984c1c8c8d1cb514527c4f60e596c863d5dbb48 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Mon, 10 Mar 2025 18:12:11 +0100 Subject: [PATCH] Fix crash when inserting characters longer than one byte in the text input for byte threshold notification setting --- src/notifications/types/notifications.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/notifications/types/notifications.rs b/src/notifications/types/notifications.rs index adaf7ea4..1705550d 100644 --- a/src/notifications/types/notifications.rs +++ b/src/notifications/types/notifications.rs @@ -101,16 +101,17 @@ impl BytesNotification { let default = existing.unwrap_or_default(); let mut byte_multiple_inserted = ByteMultiple::B; - let new_threshold = if value.is_empty() { + let chars: Vec = value.trim().chars().collect(); + let new_threshold = if chars.is_empty() { 0 - } else if !value.trim().chars().map(char::is_numeric).any(|x| !x) { + } else if !chars.iter().map(|c| char::is_numeric(*c)).any(|x| !x) { // no multiple value.parse::().unwrap_or(default.previous_threshold) } else { // multiple - let last_char = value.chars().last().unwrap(); - byte_multiple_inserted = ByteMultiple::from_char(last_char); - let without_multiple = value[0..value.len() - 1].trim().to_string(); + let last_char = chars.last().unwrap(); + byte_multiple_inserted = ByteMultiple::from_char(*last_char); + let without_multiple: String = chars[0..chars.len() - 1].iter().collect(); if without_multiple.parse::().is_ok() && TryInto::::try_into( without_multiple.parse::().unwrap() @@ -182,13 +183,11 @@ mod tests { previous_threshold: 123, threshold: Some(123), byte_multiple: ByteMultiple::B, ..BytesNotification::default() })] #[case("500k", BytesNotification { previous_threshold: 500_000, threshold: Some(500_000),byte_multiple: ByteMultiple::KB, ..BytesNotification::default() })] - #[case("420 m", BytesNotification { + #[case("420m", BytesNotification { previous_threshold: 420_000_000, threshold: Some(420_000_000),byte_multiple: ByteMultiple::MB, ..BytesNotification::default() })] - #[case("foob@r", BytesNotification{ - threshold: Some(800000), - ..Default::default() - })] - #[case(" 888 g", BytesNotification { + #[case("744Ь", BytesNotification { + previous_threshold: 744, threshold: Some(744),byte_multiple: ByteMultiple::B, ..BytesNotification::default() })] + #[case("888g", BytesNotification { previous_threshold: 888_000_000_000, threshold: Some(888_000_000_000),byte_multiple: ByteMultiple::GB, ..BytesNotification::default() })] fn test_can_instantiate_bytes_notification_from_string( #[case] input: &str,