Skip to content

Commit

Permalink
fix: ensure empty cairo0 array can be deserialized (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm authored Apr 24, 2024
1 parent ef7e70c commit 07ba9c9
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions crates/cairo-serde/src/types/array_legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ where

fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result<Self::RustType> {
if offset >= felts.len() {
return Err(Error::Deserialize(format!(
"Buffer too short to deserialize an array: offset ({}) : buffer {:?}",
offset, felts,
)));
// As the length of cairo 0 arrays is not included in the serialized form of the array,
// we don't have much choice here to return an empty array instead of an error.
return Ok(CairoArrayLegacy(vec![]));
}

let mut out: Vec<RT> = vec![];
Expand Down Expand Up @@ -96,4 +95,12 @@ mod tests {
assert_eq!(a.0[2], felt!("3"));
assert_eq!(a.0[3], felt!("4"));
}

#[test]
fn empty_array() {
// Array with only the length that is 0 (an other field as we're in cairo 0).
// So the deserialization of the array starts at index 1.
let serialized = vec![FieldElement::ZERO];
let _a = CairoArrayLegacy::<FieldElement>::cairo_deserialize(&serialized, 1).unwrap();
}
}

0 comments on commit 07ba9c9

Please sign in to comment.