]> khamidou — GitHub mirrors - safetensors.git/commitdiff
Disallowing zero-width tensor. (#205)
authorNicolas Patry <patry.nicolas@protonmail.com>
Tue, 28 Mar 2023 13:45:39 +0000 (15:45 +0200)
committerGitHub <noreply@github.com>
Tue, 28 Mar 2023 13:45:39 +0000 (15:45 +0200)
* Disallowing zero-width tensor.

* Fix test.

safetensors/src/tensor.rs

index 5c22e5d913079954063634ddf5933870aa2a6110..d8a6a258c8f67c10a9ba39fe329ba0cd345388dd 100644 (file)
@@ -459,7 +459,7 @@ impl Metadata {
         let mut start = 0;
         for (i, info) in self.tensors.iter().enumerate() {
             let (s, e) = info.data_offsets;
-            if s != start || e < s {
+            if s != start || e <= s {
                 let tensor_name = self
                     .index_map
                     .iter()
@@ -1031,4 +1031,16 @@ mod tests {
             _ => panic!("This should not be able to be deserialized"),
         }
     }
+
+    #[test]
+    fn test_zero_sized_tensor() {
+        let serialized = b"<\x00\x00\x00\x00\x00\x00\x00{\"test\":{\"dtype\":\"I32\",\"shape\":[2,0],\"data_offsets\":[0, 0]}}\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
+
+        match SafeTensors::deserialize(serialized) {
+            Err(SafeTensorError::InvalidOffset(_)) => {
+                // Yes we have the correct error
+            }
+            _ => panic!("This should not be able to be deserialized"),
+        }
+    }
 }