Browse Source

Start using boxed slices in a few places

Benjamin Sago 3 years ago
parent
commit
a6c375a91e
3 changed files with 10 additions and 10 deletions
  1. 4 4
      dns/src/record/uri.rs
  2. 5 5
      dns/src/strings.rs
  3. 1 1
      dns/src/wire.rs

+ 4 - 4
dns/src/record/uri.rs

@@ -25,7 +25,7 @@ pub struct URI {
 
     /// The URI contained in the record. Since all we are doing is displaying
     /// it to the user, we do not need to parse it for accuracy.
-    pub target: Vec<u8>,
+    pub target: Box<[u8]>,
 }
 
 impl Wire for URI {
@@ -47,7 +47,7 @@ impl Wire for URI {
         }
 
         let remaining_length = stated_length - 4;
-        let mut target = vec![0_u8; usize::from(remaining_length)];
+        let mut target = vec![0_u8; usize::from(remaining_length)].into_boxed_slice();
         c.read_exact(&mut target)?;
         trace!("Parsed target -> {:?}", String::from_utf8_lossy(&target));
 
@@ -74,7 +74,7 @@ mod test {
                    URI {
                        priority: 10,
                        weight: 16,
-                       target: b"https://rfcs.io/".to_vec(),
+                       target: Box::new(*b"https://rfcs.io/"),
                    });
     }
 
@@ -90,7 +90,7 @@ mod test {
                    URI {
                        priority: 10,
                        weight: 16,
-                       target: b"/".to_vec(),
+                       target: Box::new(*b"/"),
                    });
     }
 

+ 5 - 5
dns/src/strings.rs

@@ -165,14 +165,14 @@ fn read_string_recursive(labels: &mut Labels, c: &mut Cursor<&[u8]>, recursions:
 
             if recursions.contains(&offset) {
                 warn!("Hit previous offset ({}) decoding string", offset);
-                return Err(WireError::TooMuchRecursion(recursions.clone()));
+                return Err(WireError::TooMuchRecursion(recursions.clone().into_boxed_slice()));
             }
 
             recursions.push(offset);
 
             if recursions.len() >= RECURSION_LIMIT {
                 warn!("Hit recursion limit ({}) decoding string", RECURSION_LIMIT);
-                return Err(WireError::TooMuchRecursion(recursions.clone()));
+                return Err(WireError::TooMuchRecursion(recursions.clone().into_boxed_slice()));
             }
 
             trace!("Backtracking to offset {}", offset);
@@ -287,7 +287,7 @@ mod test {
         ];
 
         assert_eq!(Cursor::new(buf).read_labels(),
-                   Err(WireError::TooMuchRecursion(vec![ 0 ])));
+                   Err(WireError::TooMuchRecursion(Box::new([ 0 ]))));
     }
 
     #[test]
@@ -300,7 +300,7 @@ mod test {
         let mut cursor = Cursor::new(buf);
 
         assert_eq!(cursor.read_labels(),
-                   Err(WireError::TooMuchRecursion(vec![ 2, 0 ])));
+                   Err(WireError::TooMuchRecursion(Box::new([ 2, 0 ]))));
     }
 
     #[test]
@@ -320,6 +320,6 @@ mod test {
         let mut cursor = Cursor::new(buf);
 
         assert_eq!(cursor.read_labels(),
-                   Err(WireError::TooMuchRecursion(vec![ 2, 4, 6, 8, 10, 12, 14, 16 ])));
+                   Err(WireError::TooMuchRecursion(Box::new([ 2, 4, 6, 8, 10, 12, 14, 16 ]))));
     }
 }

+ 1 - 1
dns/src/wire.rs

@@ -401,7 +401,7 @@ pub enum WireError {
 
     /// When the data contained a string containing a cycle of pointers.
     /// Contains the vector of indexes that was being checked.
-    TooMuchRecursion(Vec<u16>),
+    TooMuchRecursion(Box<[u16]>),
 
     /// When the data contained a string with a pointer to an index outside of
     /// the packet. Contains the invalid index.