Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporary revert of the breaking change (keep it for 0.4.0). #336

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions safetensors/src/tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,10 @@ impl<'data> SafeTensors<'data> {
let string =
std::str::from_utf8(&buffer[8..stop]).map_err(|_| SafeTensorError::InvalidHeader)?;
// Assert the string starts with {
if !string.starts_with('{') {
return Err(SafeTensorError::InvalidHeaderStart);
}
// NOTE: Add when we move to 0.4.0
// if !string.starts_with('{') {
// return Err(SafeTensorError::InvalidHeaderStart);
// }
let metadata: Metadata = serde_json::from_str(string)
.map_err(|_| SafeTensorError::InvalidHeaderDeserialization)?;
let buffer_end = metadata.validate()?;
Expand Down Expand Up @@ -1119,17 +1120,19 @@ mod tests {
assert_eq!(loaded.len(), 0);
}

#[test]
/// Test that the JSON header must begin with a `{` character.
fn test_whitespace_start_padded_header_is_not_allowed() {
let serialized = b"\x06\x00\x00\x00\x00\x00\x00\x00\x09\x0A{}\x0D\x20";
match SafeTensors::deserialize(serialized) {
Err(SafeTensorError::InvalidHeaderStart) => {
// Correct error
},
_ => panic!("This should not be able to be deserialized"),
}
}
// Reserver for 0.4.0
// #[test]
// /// Test that the JSON header must begin with a `{` character.
// fn test_whitespace_start_padded_header_is_not_allowed() {
// let serialized = b"\x06\x00\x00\x00\x00\x00\x00\x00\x09\x0A{}\x0D\x20";
// match SafeTensors::deserialize(serialized) {
// Err(SafeTensorError::InvalidHeaderStart) => {
// // Correct error
// }
// _ => 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]}}";
Expand Down
Loading