Skip to content

Commit c43e535

Browse files
committed
Simplify DecodeError enum by removing some useless distinctions
1 parent 66fbc66 commit c43e535

File tree

5 files changed

+17
-30
lines changed

5 files changed

+17
-30
lines changed

fuzz/fuzz_targets/router_target.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,12 @@ pub fn do_test(data: &[u8]) {
125125
match <($MsgType)>::read(&mut reader) {
126126
Ok(msg) => msg,
127127
Err(e) => match e {
128-
msgs::DecodeError::UnknownRealmByte => return,
128+
msgs::DecodeError::UnknownVersion => return,
129129
msgs::DecodeError::UnknownRequiredFeature => return,
130-
msgs::DecodeError::BadPublicKey => return,
131-
msgs::DecodeError::BadSignature => return,
132-
msgs::DecodeError::BadText => return,
130+
msgs::DecodeError::InvalidValue => return,
133131
msgs::DecodeError::ExtraAddressesPerType => return,
134132
msgs::DecodeError::BadLengthDescriptor => return,
135133
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
136-
msgs::DecodeError::InvalidValue => panic!("Should not happen with p2p message decoding"),
137134
msgs::DecodeError::Io(e) => panic!(format!("{}", e)),
138135
}
139136
}

src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ impl ChannelManager {
806806
match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..])) {
807807
Err(err) => {
808808
let error_code = match err {
809-
msgs::DecodeError::UnknownRealmByte => 0x4000 | 1,
809+
msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
810810
_ => 0x2000 | 2, // Should never happen
811811
};
812812
return_err!("Unable to decode our hop data", error_code, &[0;0]);

src/ln/msgs.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ use util::ser::{Readable, Writeable, Writer};
3030
/// An error in decoding a message or struct.
3131
#[derive(Debug)]
3232
pub enum DecodeError {
33-
/// Unknown realm byte in an OnionHopData packet
34-
UnknownRealmByte,
33+
/// A version byte specified something we don't know how to handle.
34+
/// Includes unknown realm byte in an OnionHopData packet
35+
UnknownVersion,
3536
/// Unknown feature mandating we fail to parse message
3637
UnknownRequiredFeature,
37-
/// Failed to decode a public key (ie it's invalid)
38-
BadPublicKey,
39-
/// Failed to decode a signature (ie it's invalid)
40-
BadSignature,
41-
/// Value expected to be text wasn't decodable as text
42-
BadText,
38+
/// Value was invalid, eg a byte which was supposed to be a bool was something other than a 0
39+
/// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, etc
40+
InvalidValue,
4341
/// Buffer too short
4442
ShortRead,
4543
/// node_announcement included more than one address of a given type!
@@ -49,8 +47,6 @@ pub enum DecodeError {
4947
BadLengthDescriptor,
5048
/// Error from std::io
5149
Io(::std::io::Error),
52-
/// 1 or 0 is not found for boolean value
53-
InvalidValue,
5450
}
5551

5652
/// Tracks localfeatures which are only in init messages
@@ -614,16 +610,13 @@ pub(crate) struct OnionErrorPacket {
614610
impl Error for DecodeError {
615611
fn description(&self) -> &str {
616612
match *self {
617-
DecodeError::UnknownRealmByte => "Unknown realm byte in Onion packet",
613+
DecodeError::UnknownVersion => "Unknown realm byte in Onion packet",
618614
DecodeError::UnknownRequiredFeature => "Unknown required feature preventing decode",
619-
DecodeError::BadPublicKey => "Invalid public key in packet",
620-
DecodeError::BadSignature => "Invalid signature in packet",
621-
DecodeError::BadText => "Invalid text in packet",
615+
DecodeError::InvalidValue => "Nonsense bytes didn't map to the type they were interpreted as",
622616
DecodeError::ShortRead => "Packet extended beyond the provided bytes",
623617
DecodeError::ExtraAddressesPerType => "More than one address of a single type",
624618
DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
625619
DecodeError::Io(ref e) => e.description(),
626-
DecodeError::InvalidValue => "0 or 1 is not found for boolean",
627620
}
628621
}
629622
}
@@ -919,7 +912,7 @@ impl<R: Read> Readable<R> for OnionHopData {
919912
realm: {
920913
let r: u8 = Readable::read(r)?;
921914
if r != 0 {
922-
return Err(DecodeError::UnknownRealmByte);
915+
return Err(DecodeError::UnknownVersion);
923916
}
924917
r
925918
},
@@ -1087,7 +1080,7 @@ impl<R: Read> Readable<R> for ErrorMessage {
10871080
sz = cmp::min(data_len, sz);
10881081
match String::from_utf8(data[..sz as usize].to_vec()) {
10891082
Ok(s) => s,
1090-
Err(_) => return Err(DecodeError::BadText),
1083+
Err(_) => return Err(DecodeError::InvalidValue),
10911084
}
10921085
}
10931086
})

src/ln/peer_handler.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,22 +374,19 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
374374
Ok(x) => x,
375375
Err(e) => {
376376
match e {
377-
msgs::DecodeError::UnknownRealmByte => return Err(PeerHandleError{ no_connection_possible: false }),
377+
msgs::DecodeError::UnknownVersion => return Err(PeerHandleError{ no_connection_possible: false }),
378378
msgs::DecodeError::UnknownRequiredFeature => {
379379
log_debug!(self, "Got a channel/node announcement with an known required feature flag, you may want to udpate!");
380380
continue;
381381
},
382-
msgs::DecodeError::BadPublicKey => return Err(PeerHandleError{ no_connection_possible: false }),
383-
msgs::DecodeError::BadSignature => return Err(PeerHandleError{ no_connection_possible: false }),
384-
msgs::DecodeError::BadText => return Err(PeerHandleError{ no_connection_possible: false }),
382+
msgs::DecodeError::InvalidValue => return Err(PeerHandleError{ no_connection_possible: false }),
385383
msgs::DecodeError::ShortRead => return Err(PeerHandleError{ no_connection_possible: false }),
386384
msgs::DecodeError::ExtraAddressesPerType => {
387385
log_debug!(self, "Error decoding message, ignoring due to lnd spec incompatibility. See https://github.com/lightningnetwork/lnd/issues/1407");
388386
continue;
389387
},
390388
msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
391389
msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }),
392-
msgs::DecodeError::InvalidValue => panic!("should not happen with message decoding"),
393390
}
394391
}
395392
};

src/util/ser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl<R: Read> Readable<R> for PublicKey {
295295
let buf: [u8; 33] = Readable::read(r)?;
296296
match PublicKey::from_slice(&Secp256k1::without_caps(), &buf) {
297297
Ok(key) => Ok(key),
298-
Err(_) => return Err(DecodeError::BadPublicKey),
298+
Err(_) => return Err(DecodeError::InvalidValue),
299299
}
300300
}
301301
}
@@ -324,7 +324,7 @@ impl<R: Read> Readable<R> for Signature {
324324
let buf: [u8; 64] = Readable::read(r)?;
325325
match Signature::from_compact(&Secp256k1::without_caps(), &buf) {
326326
Ok(sig) => Ok(sig),
327-
Err(_) => return Err(DecodeError::BadSignature),
327+
Err(_) => return Err(DecodeError::InvalidValue),
328328
}
329329
}
330330
}

0 commit comments

Comments
 (0)