difftreelog
Fix check_property_key
in: master
2 files changed
pallets/common/src/lib.rsdiffbeforeafterboth378 /// Only ASCII letters, digits, and '_', '-' are allowed378 /// Only ASCII letters, digits, and '_', '-' are allowed379 InvalidCharacterInPropertyKey,379 InvalidCharacterInPropertyKey,380381 /// Empty property keys are forbidden382 EmptyPropertyKey,380 }383 }381384382 #[pallet::storage]385 #[pallet::storage]1226 PropertiesError::NoSpaceForProperty => Self::NoSpaceForProperty,1229 PropertiesError::NoSpaceForProperty => Self::NoSpaceForProperty,1227 PropertiesError::PropertyLimitReached => Self::PropertyLimitReached,1230 PropertiesError::PropertyLimitReached => Self::PropertyLimitReached,1228 PropertiesError::InvalidCharacterInPropertyKey => Self::InvalidCharacterInPropertyKey,1231 PropertiesError::InvalidCharacterInPropertyKey => Self::InvalidCharacterInPropertyKey,1232 PropertiesError::EmptyPropertyKey => Self::EmptyPropertyKey,1229 }1233 }1230 }1234 }1231}1235}primitives/data-structs/src/lib.rsdiffbeforeafterboth--- 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)
}
}