From 1f2655a7f2ac44ec72c8efd66dc498dfcbd59fe4 Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 29 Jul 2021 21:54:47 +0100 Subject: [PATCH] Basic message de/serialisation test passes. (Value -> Message -> SerializedMessage -> Bytes -> SerializedMessage -> Message -> Value). No encryption tests yet. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TODO: Better size checks. TODO: Test message de/serialisation with encryption and/or signing enabled. Fortune for rsh's current commit: Half curse − 半凶 --- src/message.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) 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) => { {