From 599b5140ca9169de9799677586896b537b68eaf1 Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Thu, 12 May 2022 15:50:45 +0000 Subject: [PATCH] Fix check_property_key --- --- a/pallets/common/src/lib.rs +++ b/pallets/common/src/lib.rs @@ -377,6 +377,9 @@ /// Only ASCII letters, digits, and '_', '-' are allowed InvalidCharacterInPropertyKey, + + /// Empty property keys are forbidden + EmptyPropertyKey, } #[pallet::storage] @@ -1226,6 +1229,7 @@ PropertiesError::NoSpaceForProperty => Self::NoSpaceForProperty, PropertiesError::PropertyLimitReached => Self::PropertyLimitReached, PropertiesError::InvalidCharacterInPropertyKey => Self::InvalidCharacterInPropertyKey, + PropertiesError::EmptyPropertyKey => Self::EmptyPropertyKey, } } } --- a/primitives/data-structs/src/lib.rs +++ b/primitives/data-structs/src/lib.rs @@ -683,6 +683,7 @@ NoSpaceForProperty, PropertyLimitReached, InvalidCharacterInPropertyKey, + EmptyPropertyKey, } pub trait TrySet: Sized { @@ -728,12 +729,17 @@ } fn check_property_key(key: &PropertyKey) -> Result<(), PropertiesError> { - let key_str = sp_std::str::from_utf8(key.as_slice()) - .map_err(|_| PropertiesError::InvalidCharacterInPropertyKey)?; + if key.is_empty() { + return Err(PropertiesError::EmptyPropertyKey); + } - for ch in key_str.chars() { - if !ch.is_ascii_alphanumeric() && ch != '_' && ch != '-' { - return Err(PropertiesError::InvalidCharacterInPropertyKey); + for byte in key.as_slice().iter() { + match char::from_u32(*byte as u32) { + Some(ch) + if ch.is_ascii_alphanumeric() + || ch == '_' + || ch == '-' => { /* OK */ }, + _ => return Err(PropertiesError::InvalidCharacterInPropertyKey) } } -- gitstuff