From 8322c756cb3574d74cf1b88893dad14ec5be57ad Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 16 Sep 2018 13:50:42 -0400 Subject: [PATCH] Stop checking size > 64KB in serialization This removes a bunch of potentially new error handling in writers and the checks were kinda useless anyway - in normal operation we unwrap()ed anyway, and we're gonna want to use the serializtion framework for ChannelMonitor/ChannelManager serialization, which may generate things larger than 64KB anyway. --- src/ln/msgs.rs | 20 +++++++++---------- src/util/ser.rs | 45 ++++++++++++++++++------------------------ src/util/ser_macros.rs | 4 ++-- 3 files changed, 31 insertions(+), 38 deletions(-) diff --git a/src/ln/msgs.rs b/src/ln/msgs.rs index 843d08729..5100ed79c 100644 --- a/src/ln/msgs.rs +++ b/src/ln/msgs.rs @@ -743,7 +743,7 @@ impl_writeable!(AnnouncementSignatures, 32+8+64*2, { }); impl Writeable for ChannelReestablish { - fn write(&self, w: &mut W) -> Result<(), DecodeError> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(if self.data_loss_protect.is_some() { 32+2*8+33+32 } else { 32+2*8 }); self.channel_id.write(w)?; self.next_local_commitment_number.write(w)?; @@ -906,7 +906,7 @@ impl_writeable_len_match!(OnionErrorPacket, { }); impl Writeable for OnionPacket { - fn write(&self, w: &mut W) -> Result<(), DecodeError> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(1 + 33 + 20*65 + 32); self.version.write(w)?; match self.public_key { @@ -944,7 +944,7 @@ impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, { }); impl Writeable for OnionRealm0HopData { - fn write(&self, w: &mut W) -> Result<(), DecodeError> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(32); self.short_channel_id.write(w)?; self.amt_to_forward.write(w)?; @@ -969,7 +969,7 @@ impl Readable for OnionRealm0HopData { } impl Writeable for OnionHopData { - fn write(&self, w: &mut W) -> Result<(), DecodeError> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(65); self.realm.write(w)?; self.data.write(w)?; @@ -995,7 +995,7 @@ impl Readable for OnionHopData { } impl Writeable for Ping { - fn write(&self, w: &mut W) -> Result<(), DecodeError> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(self.byteslen as usize + 4); self.ponglen.write(w)?; vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write @@ -1017,7 +1017,7 @@ impl Readable for Ping { } impl Writeable for Pong { - fn write(&self, w: &mut W) -> Result<(), DecodeError> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(self.byteslen as usize + 2); vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write Ok(()) @@ -1037,7 +1037,7 @@ impl Readable for Pong { } impl Writeable for UnsignedChannelAnnouncement { - fn write(&self, w: &mut W) -> Result<(), DecodeError> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(2 + 2*32 + 4*33 + self.features.flags.len() + self.excess_data.len()); self.features.write(w)?; self.chain_hash.write(w)?; @@ -1088,7 +1088,7 @@ impl_writeable_len_match!(ChannelAnnouncement, { }); impl Writeable for UnsignedChannelUpdate { - fn write(&self, w: &mut W) -> Result<(), DecodeError> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(64 + self.excess_data.len()); self.chain_hash.write(w)?; self.short_channel_id.write(w)?; @@ -1132,7 +1132,7 @@ impl_writeable_len_match!(ChannelUpdate, { }); impl Writeable for ErrorMessage { - fn write(&self, w: &mut W) -> Result<(), DecodeError> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(32 + 2 + self.data.len()); self.channel_id.write(w)?; (self.data.len() as u16).write(w)?; @@ -1160,7 +1160,7 @@ impl Readable for ErrorMessage { } impl Writeable for UnsignedNodeAnnouncement { - fn write(&self, w: &mut W) -> Result<(), DecodeError> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(64 + 76 + self.features.flags.len() + self.addresses.len()*38 + self.excess_address_data.len() + self.excess_data.len()); self.features.write(w)?; self.timestamp.write(w)?; diff --git a/src/util/ser.rs b/src/util/ser.rs index fe92332df..18aa7eee9 100644 --- a/src/util/ser.rs +++ b/src/util/ser.rs @@ -38,7 +38,7 @@ impl Writer for W { /// A trait that various rust-lightning types implement allowing them to be written out to a Writer pub trait Writeable { /// Writes self out to the given Writer - fn write(&self, writer: &mut W) -> Result<(), DecodeError>; + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error>; } /// A trait that various rust-lightning types implement allowing them to be read in from a Read @@ -54,8 +54,8 @@ macro_rules! impl_writeable_primitive { ($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => { impl Writeable for $val_type { #[inline] - fn write(&self, writer: &mut W) -> Result<(), DecodeError> { - Ok(writer.write_all(&$meth_write(*self))?) + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + writer.write_all(&$meth_write(*self)) } } impl Readable for $val_type { @@ -75,8 +75,8 @@ impl_writeable_primitive!(u16, be16_to_array, 2, slice_to_be16); impl Writeable for u8 { #[inline] - fn write(&self, writer: &mut W) -> Result<(), DecodeError> { - Ok(writer.write_all(&[*self])?) + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + writer.write_all(&[*self]) } } impl Readable for u8 { @@ -90,8 +90,8 @@ impl Readable for u8 { impl Writeable for bool { #[inline] - fn write(&self, writer: &mut W) -> Result<(), DecodeError> { - Ok(writer.write_all(&[if *self {1} else {0}])?) + fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + writer.write_all(&[if *self {1} else {0}]) } } impl Readable for bool { @@ -112,9 +112,8 @@ macro_rules! impl_array { impl Writeable for [u8; $size] { #[inline] - fn write(&self, w: &mut W) -> Result<(), DecodeError> { - w.write_all(self)?; - Ok(()) + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + w.write_all(self) } } @@ -143,7 +142,7 @@ impl Writeable for HashMap V: Writeable { #[inline] - fn write(&self, w: &mut W) -> Result<(), DecodeError> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { (self.len() as u16).write(w)?; for (key, value) in self.iter() { key.write(w)?; @@ -172,9 +171,9 @@ impl Readable for HashMap // Vectors impl Writeable for Vec { #[inline] - fn write(&self, w: &mut W) -> Result<(), DecodeError> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { (self.len() as u16).write(w)?; - Ok(w.write_all(&self)?) + w.write_all(&self) } } @@ -190,13 +189,7 @@ impl Readable for Vec { } impl Writeable for Vec { #[inline] - fn write(&self, w: &mut W) -> Result<(), DecodeError> { - let byte_size = (self.len() as usize) - .checked_mul(33) - .ok_or(DecodeError::BadLengthDescriptor)?; - if byte_size > MAX_BUF_SIZE { - return Err(DecodeError::BadLengthDescriptor); - } + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { (self.len() as u16).write(w)?; for e in self.iter() { e.write(w)?; @@ -222,9 +215,9 @@ impl Readable for Vec { } impl Writeable for Script { - fn write(&self, w: &mut W) -> Result<(), DecodeError> { + fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { (self.len() as u16).write(w)?; - Ok(w.write_all(self.as_bytes())?) + w.write_all(self.as_bytes()) } } @@ -238,7 +231,7 @@ impl Readable for Script { } impl Writeable for Option