From 07ba9c91baa2f6aa2bc850b6699ffc2266be9a13 Mon Sep 17 00:00:00 2001 From: glihm Date: Tue, 23 Apr 2024 23:04:33 -0600 Subject: [PATCH] fix: ensure empty cairo0 array can be deserialized (#26) --- crates/cairo-serde/src/types/array_legacy.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/cairo-serde/src/types/array_legacy.rs b/crates/cairo-serde/src/types/array_legacy.rs index 9595228..8227185 100644 --- a/crates/cairo-serde/src/types/array_legacy.rs +++ b/crates/cairo-serde/src/types/array_legacy.rs @@ -50,10 +50,9 @@ where fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { 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 = vec![]; @@ -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::::cairo_deserialize(&serialized, 1).unwrap(); + } }