From: Nicolas Patry Date: Tue, 28 Mar 2023 13:45:39 +0000 (+0200) Subject: Disallowing zero-width tensor. (#205) X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=446f7ade0b068c0c8b5e81c80490c2408b755fd7;p=safetensors.git Disallowing zero-width tensor. (#205) * Disallowing zero-width tensor. * Fix test. --- diff --git a/safetensors/src/tensor.rs b/safetensors/src/tensor.rs index 5c22e5d..d8a6a25 100644 --- a/safetensors/src/tensor.rs +++ b/safetensors/src/tensor.rs @@ -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"), + } + } }