diff --git a/src/message.rs b/src/message.rs index 939b3e1..9b02b92 100644 --- a/src/message.rs +++ b/src/message.rs @@ -260,10 +260,13 @@ impl SerializedMessage }; (: $ser:expr) => { { - + let ser = serde_cbor::to_vec($ser)?; + write!(u64::try_from(ser.len())?.to_be_bytes()); + write!(ser); + /* let mut w2 = WriteCounter(0, &mut writer); serde_cbor::to_writer(&mut w2, $ser)?; - w+=w2.0; + w+=w2.0;*/ } }; } @@ -305,10 +308,18 @@ impl SerializedMessage } }; (: $ser:ty) => { - serde_cbor::from_reader::<$ser, _>(&mut reader).wrap_err(eyre!("Failed to deserialise {} from reader", std::any::type_name::<$ser>()))? - }; - (:) => { - serde_cbor::from_reader(&mut reader).wrap_err(eyre!("Failed to deserialise type from reader"))? + { + let mut len = [0u8; std::mem::size_of::()]; + read!(&mut len[..]); + let len = usize::try_from(u64::from_be_bytes(len))?; + //TODO: Find realistic max size for `$ser`. + if len > MAX_ALLOC_SIZE { + return Err(eyre!("Invalid length read: {}", len)); + } + let mut de = Vec::with_capacity(len); + read!(&mut de, len); + serde_cbor::from_slice::<$ser>(&de[..]).wrap_err(eyre!("Failed to deserialise {} from reader", std::any::type_name::<$ser>()))? + } }; ($into:expr, $num:expr) => { {