Skip to content

Commit a399d4f

Browse files
committed
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.
1 parent 19c445b commit a399d4f

File tree

3 files changed

+31
-38
lines changed

3 files changed

+31
-38
lines changed

src/ln/msgs.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ impl_writeable!(AnnouncementSignatures, 32+8+64*2, {
743743
});
744744

745745
impl<W: Writer> Writeable<W> for ChannelReestablish {
746-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
746+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
747747
w.size_hint(if self.data_loss_protect.is_some() { 32+2*8+33+32 } else { 32+2*8 });
748748
self.channel_id.write(w)?;
749749
self.next_local_commitment_number.write(w)?;
@@ -906,7 +906,7 @@ impl_writeable_len_match!(OnionErrorPacket, {
906906
});
907907

908908
impl<W: Writer> Writeable<W> for OnionPacket {
909-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
909+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
910910
w.size_hint(1 + 33 + 20*65 + 32);
911911
self.version.write(w)?;
912912
match self.public_key {
@@ -944,7 +944,7 @@ impl_writeable!(UpdateAddHTLC, 32+8+8+32+4+1366, {
944944
});
945945

946946
impl<W: Writer> Writeable<W> for OnionRealm0HopData {
947-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
947+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
948948
w.size_hint(32);
949949
self.short_channel_id.write(w)?;
950950
self.amt_to_forward.write(w)?;
@@ -969,7 +969,7 @@ impl<R: Read> Readable<R> for OnionRealm0HopData {
969969
}
970970

971971
impl<W: Writer> Writeable<W> for OnionHopData {
972-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
972+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
973973
w.size_hint(65);
974974
self.realm.write(w)?;
975975
self.data.write(w)?;
@@ -995,7 +995,7 @@ impl<R: Read> Readable<R> for OnionHopData {
995995
}
996996

997997
impl<W: Writer> Writeable<W> for Ping {
998-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
998+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
999999
w.size_hint(self.byteslen as usize + 4);
10001000
self.ponglen.write(w)?;
10011001
vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
@@ -1017,7 +1017,7 @@ impl<R: Read> Readable<R> for Ping {
10171017
}
10181018

10191019
impl<W: Writer> Writeable<W> for Pong {
1020-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
1020+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
10211021
w.size_hint(self.byteslen as usize + 2);
10221022
vec![0u8; self.byteslen as usize].write(w)?; // size-unchecked write
10231023
Ok(())
@@ -1037,7 +1037,7 @@ impl<R: Read> Readable<R> for Pong {
10371037
}
10381038

10391039
impl<W: Writer> Writeable<W> for UnsignedChannelAnnouncement {
1040-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
1040+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
10411041
w.size_hint(2 + 2*32 + 4*33 + self.features.flags.len() + self.excess_data.len());
10421042
self.features.write(w)?;
10431043
self.chain_hash.write(w)?;
@@ -1088,7 +1088,7 @@ impl_writeable_len_match!(ChannelAnnouncement, {
10881088
});
10891089

10901090
impl<W: Writer> Writeable<W> for UnsignedChannelUpdate {
1091-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
1091+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
10921092
w.size_hint(64 + self.excess_data.len());
10931093
self.chain_hash.write(w)?;
10941094
self.short_channel_id.write(w)?;
@@ -1132,7 +1132,7 @@ impl_writeable_len_match!(ChannelUpdate, {
11321132
});
11331133

11341134
impl<W: Writer> Writeable<W> for ErrorMessage {
1135-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
1135+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
11361136
w.size_hint(32 + 2 + self.data.len());
11371137
self.channel_id.write(w)?;
11381138
(self.data.len() as u16).write(w)?;
@@ -1160,7 +1160,7 @@ impl<R: Read> Readable<R> for ErrorMessage {
11601160
}
11611161

11621162
impl<W: Writer> Writeable<W> for UnsignedNodeAnnouncement {
1163-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
1163+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
11641164
w.size_hint(64 + 76 + self.features.flags.len() + self.addresses.len()*38 + self.excess_address_data.len() + self.excess_data.len());
11651165
self.features.write(w)?;
11661166
self.timestamp.write(w)?;

src/util/ser.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<W: ::std::io::Write> Writer for W {
3838
/// A trait that various rust-lightning types implement allowing them to be written out to a Writer
3939
pub trait Writeable<W: Writer> {
4040
/// Writes self out to the given Writer
41-
fn write(&self, writer: &mut W) -> Result<(), DecodeError>;
41+
fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error>;
4242
}
4343

4444
/// 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 {
5454
($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => {
5555
impl<W: Writer> Writeable<W> for $val_type {
5656
#[inline]
57-
fn write(&self, writer: &mut W) -> Result<(), DecodeError> {
58-
Ok(writer.write_all(&$meth_write(*self))?)
57+
fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
58+
writer.write_all(&$meth_write(*self))
5959
}
6060
}
6161
impl<R: Read> Readable<R> for $val_type {
@@ -75,8 +75,8 @@ impl_writeable_primitive!(u16, be16_to_array, 2, slice_to_be16);
7575

7676
impl<W: Writer> Writeable<W> for u8 {
7777
#[inline]
78-
fn write(&self, writer: &mut W) -> Result<(), DecodeError> {
79-
Ok(writer.write_all(&[*self])?)
78+
fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
79+
writer.write_all(&[*self])
8080
}
8181
}
8282
impl<R: Read> Readable<R> for u8 {
@@ -90,8 +90,8 @@ impl<R: Read> Readable<R> for u8 {
9090

9191
impl<W: Writer> Writeable<W> for bool {
9292
#[inline]
93-
fn write(&self, writer: &mut W) -> Result<(), DecodeError> {
94-
Ok(writer.write_all(&[if *self {1} else {0}])?)
93+
fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
94+
writer.write_all(&[if *self {1} else {0}])
9595
}
9696
}
9797
impl<R: Read> Readable<R> for bool {
@@ -112,9 +112,8 @@ macro_rules! impl_array {
112112
impl<W: Writer> Writeable<W> for [u8; $size]
113113
{
114114
#[inline]
115-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
116-
w.write_all(self)?;
117-
Ok(())
115+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
116+
w.write_all(self)
118117
}
119118
}
120119

@@ -143,7 +142,7 @@ impl<W, K, V> Writeable<W> for HashMap<K, V>
143142
V: Writeable<W>
144143
{
145144
#[inline]
146-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
145+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
147146
(self.len() as u16).write(w)?;
148147
for (key, value) in self.iter() {
149148
key.write(w)?;
@@ -172,9 +171,9 @@ impl<R, K, V> Readable<R> for HashMap<K, V>
172171
// Vectors
173172
impl<W: Writer> Writeable<W> for Vec<u8> {
174173
#[inline]
175-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
174+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
176175
(self.len() as u16).write(w)?;
177-
Ok(w.write_all(&self)?)
176+
w.write_all(&self)
178177
}
179178
}
180179

@@ -190,13 +189,7 @@ impl<R: Read> Readable<R> for Vec<u8> {
190189
}
191190
impl<W: Writer> Writeable<W> for Vec<Signature> {
192191
#[inline]
193-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
194-
let byte_size = (self.len() as usize)
195-
.checked_mul(33)
196-
.ok_or(DecodeError::BadLengthDescriptor)?;
197-
if byte_size > MAX_BUF_SIZE {
198-
return Err(DecodeError::BadLengthDescriptor);
199-
}
192+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
200193
(self.len() as u16).write(w)?;
201194
for e in self.iter() {
202195
e.write(w)?;
@@ -222,9 +215,9 @@ impl<R: Read> Readable<R> for Vec<Signature> {
222215
}
223216

224217
impl<W: Writer> Writeable<W> for Script {
225-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
218+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
226219
(self.len() as u16).write(w)?;
227-
Ok(w.write_all(self.as_bytes())?)
220+
w.write_all(self.as_bytes())
228221
}
229222
}
230223

@@ -238,7 +231,7 @@ impl<R: Read> Readable<R> for Script {
238231
}
239232

240233
impl<W: Writer> Writeable<W> for Option<Script> {
241-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
234+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
242235
if let &Some(ref script) = self {
243236
script.write(w)?;
244237
}
@@ -261,7 +254,7 @@ impl<R: Read> Readable<R> for Option<Script> {
261254
}
262255

263256
impl<W: Writer> Writeable<W> for PublicKey {
264-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
257+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
265258
self.serialize().write(w)
266259
}
267260
}
@@ -277,7 +270,7 @@ impl<R: Read> Readable<R> for PublicKey {
277270
}
278271

279272
impl<W: Writer> Writeable<W> for Sha256dHash {
280-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
273+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
281274
self.as_bytes().write(w)
282275
}
283276
}
@@ -290,7 +283,7 @@ impl<R: Read> Readable<R> for Sha256dHash {
290283
}
291284

292285
impl<W: Writer> Writeable<W> for Signature {
293-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
286+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
294287
self.serialize_compact(&Secp256k1::without_caps()).write(w)
295288
}
296289
}

src/util/ser_macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
macro_rules! impl_writeable {
22
($st:ident, $len: expr, {$($field:ident),*}) => {
33
impl<W: Writer> Writeable<W> for $st {
4-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
4+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
55
w.size_hint($len);
66
$( self.$field.write(w)?; )*
77
Ok(())
@@ -20,7 +20,7 @@ macro_rules! impl_writeable {
2020
macro_rules! impl_writeable_len_match {
2121
($st:ident, {$({$m: pat, $l: expr}),*}, {$($field:ident),*}) => {
2222
impl<W: Writer> Writeable<W> for $st {
23-
fn write(&self, w: &mut W) -> Result<(), DecodeError> {
23+
fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> {
2424
w.size_hint(match *self {
2525
$($m => $l,)*
2626
});

0 commit comments

Comments
 (0)