Jelajahi Sumber

Make sure that only normal paths can be added to the namespace

If we don't make sure that paths are normal, two paths could be added that
actually point to the same location.
Isaac Woods 5 tahun lalu
induk
melakukan
f7c96187d0
1 mengubah file dengan 27 tambahan dan 0 penghapusan
  1. 27 0
      aml_parser/src/namespace.rs

+ 27 - 0
aml_parser/src/namespace.rs

@@ -51,6 +51,7 @@ impl Namespace {
     /// instead.
     pub fn add(&mut self, path: AmlName, value: AmlValue) -> Result<AmlHandle, AmlError> {
         assert!(path.is_absolute());
+        assert!(path.is_normal());
 
         if self.name_map.contains_key(&path) {
             return Err(AmlError::NameCollision(path.clone()));
@@ -186,6 +187,12 @@ impl AmlName {
             .to_string()
     }
 
+    /// An AML path is normal if it does not contain any prefix elements ("^" characters, when
+    /// expressed as a string).
+    pub fn is_normal(&self) -> bool {
+        !self.0.contains(&NameComponent::Prefix)
+    }
+
     pub fn is_absolute(&self) -> bool {
         self.0.first() == Some(&NameComponent::Root)
     }
@@ -284,6 +291,26 @@ mod tests {
         );
     }
 
+    #[test]
+    fn test_is_normal() {
+        assert_eq!(AmlName::root().is_normal(), true);
+        assert_eq!(AmlName::from_str("\\_SB.PCI0.VGA").unwrap().is_normal(), true);
+        assert_eq!(AmlName::from_str("\\_SB.^PCI0.VGA").unwrap().is_normal(), false);
+        assert_eq!(AmlName::from_str("\\^_SB.^^PCI0.VGA").unwrap().is_normal(), false);
+        assert_eq!(AmlName::from_str("_SB.^^PCI0.VGA").unwrap().is_normal(), false);
+        assert_eq!(AmlName::from_str("_SB.PCI0.VGA").unwrap().is_normal(), true);
+    }
+
+    #[test]
+    fn test_is_absolute() {
+        assert_eq!(AmlName::root().is_absolute(), true);
+        assert_eq!(AmlName::from_str("\\_SB.PCI0.VGA").unwrap().is_absolute(), true);
+        assert_eq!(AmlName::from_str("\\_SB.^PCI0.VGA").unwrap().is_absolute(), true);
+        assert_eq!(AmlName::from_str("\\^_SB.^^PCI0.VGA").unwrap().is_absolute(), true);
+        assert_eq!(AmlName::from_str("_SB.^^PCI0.VGA").unwrap().is_absolute(), false);
+        assert_eq!(AmlName::from_str("_SB.PCI0.VGA").unwrap().is_absolute(), false);
+    }
+
     #[test]
     fn test_search_rules_apply() {
         assert_eq!(AmlName::root().search_rules_apply(), false);