Skip to content

Commit 1b8504a

Browse files
yuntaiTheBlueMatt
authored andcommitted
Implement Writer/Reader
with additional variants in DecodeError
1 parent bde48b2 commit 1b8504a

File tree

6 files changed

+359
-0
lines changed

6 files changed

+359
-0
lines changed

fuzz/fuzz_targets/channel_target.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ pub fn do_test(data: &[u8]) {
131131
msgs::DecodeError::ExtraAddressesPerType => return,
132132
msgs::DecodeError::BadLengthDescriptor => return,
133133
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
134+
msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."),
135+
msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."),
134136
}
135137
}
136138
}
@@ -154,6 +156,8 @@ pub fn do_test(data: &[u8]) {
154156
msgs::DecodeError::ExtraAddressesPerType => return,
155157
msgs::DecodeError::BadLengthDescriptor => return,
156158
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
159+
msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."),
160+
msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."),
157161
}
158162
}
159163
}

fuzz/fuzz_targets/router_target.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ pub fn do_test(data: &[u8]) {
131131
msgs::DecodeError::ExtraAddressesPerType => return,
132132
msgs::DecodeError::BadLengthDescriptor => return,
133133
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
134+
msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."),
135+
msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."),
134136
}
135137
}
136138
}

src/ln/msgs.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ pub enum DecodeError {
4343
/// A length descriptor in the packet didn't describe the later data correctly
4444
/// (currently only generated in node_announcement)
4545
BadLengthDescriptor,
46+
/// Error from std::io
47+
Io(::std::io::Error),
48+
/// Invalid value found when decoding
49+
InvalidValue,
4650
}
4751
pub trait MsgDecodable: Sized {
4852
fn decode(v: &[u8]) -> Result<Self, DecodeError>;
@@ -519,6 +523,8 @@ impl Error for DecodeError {
519523
DecodeError::ShortRead => "Packet extended beyond the provided bytes",
520524
DecodeError::ExtraAddressesPerType => "More than one address of a single type",
521525
DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
526+
DecodeError::Io(ref e) => e.description(),
527+
DecodeError::InvalidValue => "Invalid value in the bytes",
522528
}
523529
}
524530
}
@@ -534,6 +540,16 @@ impl fmt::Debug for HandleError {
534540
}
535541
}
536542

543+
impl From<::std::io::Error> for DecodeError {
544+
fn from(e: ::std::io::Error) -> Self {
545+
if e.kind() == ::std::io::ErrorKind::UnexpectedEof {
546+
DecodeError::ShortRead
547+
} else {
548+
DecodeError::Io(e)
549+
}
550+
}
551+
}
552+
537553
macro_rules! secp_pubkey {
538554
( $ctx: expr, $slice: expr ) => {
539555
match PublicKey::from_slice($ctx, $slice) {

src/ln/peer_handler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
363363
continue;
364364
},
365365
msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
366+
msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }),
367+
msgs::DecodeError::InvalidValue => return Err(PeerHandleError{ no_connection_possible: false }),
368+
msgs::DecodeError::InvalidLength => return Err(PeerHandleError{ no_connection_possible: false }),
366369
}
367370
}
368371
};

src/util/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,11 @@ pub(crate) mod test_utils;
2121
#[macro_use]
2222
pub(crate) mod macro_logger;
2323

24+
#[cfg(feature = "fuzztarget")]
25+
#[macro_use]
26+
pub mod ser;
27+
#[cfg(not(feature = "fuzztarget"))]
28+
#[macro_use]
29+
pub(crate) mod ser;
30+
2431
pub mod logger;

0 commit comments

Comments
 (0)