git.delta.rocks / unique-network / refs/commits / 599b5140ca91

difftreelog

Fix check_property_key

Daniel Shiposha2022-05-12parent: #579ff14.patch.diff
in: master

2 files changed

modifiedpallets/common/src/lib.rsdiffbeforeafterboth
378 /// Only ASCII letters, digits, and '_', '-' are allowed378 /// Only ASCII letters, digits, and '_', '-' are allowed
379 InvalidCharacterInPropertyKey,379 InvalidCharacterInPropertyKey,
380
381 /// Empty property keys are forbidden
382 EmptyPropertyKey,
380 }383 }
381384
382 #[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}
modifiedprimitives/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)
 			}
 		}