From 24925e309d5975f736302b387bfd345367e45b74 Mon Sep 17 00:00:00 2001 From: StriderDM <51991544+StriderDM@users.noreply.github.com> Date: Mon, 20 Apr 2020 11:45:45 +0200 Subject: [PATCH] Bug Fix Check if Emoji string is empty before trying to validate it. cargo fmt clippy lint Style fix Check for empty array in luhn.rs since function is public. --- base_layer/wallet/src/util/emoji.rs | 1 + base_layer/wallet/src/util/luhn.rs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/base_layer/wallet/src/util/emoji.rs b/base_layer/wallet/src/util/emoji.rs index 23ffc5f0b6..6fcb1fa3a3 100644 --- a/base_layer/wallet/src/util/emoji.rs +++ b/base_layer/wallet/src/util/emoji.rs @@ -188,6 +188,7 @@ mod test { let eid = EmojiId::from_hex("70350e09c474809209824c6e6888707b7dd09959aa227343b5106382b856f73a").unwrap(); // Valid emojiID assert!(EmojiId::is_valid(eid.as_str())); + assert_eq!(EmojiId::is_valid(""), false, "Emoji ID too short"); assert_eq!(EmojiId::is_valid("πŸ˜‚"), false, "Emoji ID too short"); assert_eq!( EmojiId::is_valid("πŸ€©βš½πŸπŸŽπŸ«πŸ€©πŸ“πŸ’”πŸ˜πŸ¦„πŸžπŸ‡πŸŒ²πŸ‡πŸŽΆπŸ πŸ§£πŸš’πŸ˜ˆπŸΈπŸ‘ŠπŸ•™πŸ€€πŸ’ŽπŸ“β›…πŸ‘”πŸ†—πŸ„πŸ‘"), diff --git a/base_layer/wallet/src/util/luhn.rs b/base_layer/wallet/src/util/luhn.rs index 170aad4b36..f1b2661bc0 100644 --- a/base_layer/wallet/src/util/luhn.rs +++ b/base_layer/wallet/src/util/luhn.rs @@ -36,6 +36,9 @@ pub fn checksum(arr: &[usize], dict_len: usize) -> usize { /// Checks whether the last digit in the array matches the checksum for the array minus the last digit. pub fn is_valid(arr: &[usize], dict_len: usize) -> bool { + if arr.len() < 2 { + return false; + } let cs = checksum(&arr[..arr.len() - 1], dict_len); cs == arr[arr.len() - 1] }