Skip to content

Commit 1d133c2

Browse files
committed
better error handling
1 parent 851c91d commit 1d133c2

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

lightning/src/ln/msgs.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -904,26 +904,44 @@ impl FromStr for NetAddress {
904904
let port: u16 = addr.port();
905905
match addr {
906906
SocketAddr::V4(addr) => {
907-
let addr = addr.ip().to_string().parse::<Ipv4Addr>()?;
908-
return Ok(NetAddress::IPv4 { addr: addr.octets(), port });
907+
let addr = addr.ip().octets();
908+
return Ok(NetAddress::IPv4 { addr, port });
909909
},
910910
SocketAddr::V6(addr) => {
911-
let addr = addr.ip().to_string().parse::<Ipv6Addr>()?;
912-
return Ok(NetAddress::IPv6 { addr: addr.octets(), port });
911+
let addr = addr.ip().octets();
912+
return Ok(NetAddress::IPv6 { addr, port });
913913
},
914914
}
915915
},
916916
Err(e) => {
917-
let trimmed_input = s.rfind(":").expect("Invalid input, needs to include seperator \":\"");
917+
let trimmed_input = match s.rfind(":") {
918+
Some(pos) => pos,
919+
None => return Err(e),
920+
};
918921
let host = &s[..trimmed_input];
919-
let port: u16 = s[trimmed_input + 1..].parse().expect("Invalid input, port needs to be u16");
922+
let port: u16 = match s[trimmed_input + 1..].parse() {
923+
Ok(port) => port,
924+
Err(_) => return Err(e),
925+
};
920926
if host.ends_with(".onion") {
921-
let onion = host.split(".onion").collect::<Vec<&str>>()[0];
922-
let onion = base32::decode(base32::Alphabet::RFC4648 { padding: false }, &onion).expect("Error decoding onion address");
923-
let version = onion[0];
927+
let onion = match host.split(".onion").nth(0) {
928+
Some(onion) => onion,
929+
None => return Err(e),
930+
};
931+
let onion = match base32::decode(base32::Alphabet::RFC4648 { padding: false }, &onion) {
932+
Some(onion) => onion,
933+
None => return Err(e),
934+
};
935+
let version = match onion.get(0) {
936+
Some(version) => version,
937+
None => return Err(e),
938+
};
924939
let checksum = u16::from_be_bytes([onion[1], onion[2]]);
925-
let ed25519_pubkey = onion[3..35].try_into().expect("Invalid onion address");
926-
return Ok(NetAddress::OnionV3 { ed25519_pubkey, checksum, version, port });
940+
let ed25519_pubkey = match onion[3..35].try_into() {
941+
Ok(ed25519_pubkey) => ed25519_pubkey,
942+
Err(_) => return Err(e),
943+
};
944+
return Ok(NetAddress::OnionV3 { ed25519_pubkey, checksum, version: *version, port });
927945
}
928946

929947
if let Ok(hostname) = Hostname::try_from(host.to_string()) {

lightning/src/util/base32.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
/// Alphabet used for encoding and decoding.
2424
#[derive(Copy, Clone)]
2525
pub enum Alphabet {
26-
/// RFC4648 base32 encoding. padding: bool indicates whether to use padding.
27-
RFC4648 { padding: bool },
26+
/// RFC4648 base32 encoding.
27+
RFC4648 {
28+
/// Whether to use padding.
29+
padding: bool
30+
},
2831
/// Crockford base32 encoding.
2932
Crockford,
3033
}
@@ -88,7 +91,7 @@ const CROCKFORD_INV_ALPHABET: [i8; 43] = [
8891
18, 19, 1, 20, 21, 0, 22, 23, 24, 25, 26, -1, 27, 28, 29, 30, 31,
8992
];
9093

91-
// Decode a base32 string into a byte vector.
94+
/// Decode a base32 string into a byte vector.
9295
pub fn decode(alphabet: Alphabet, data: &str) -> Option<Vec<u8>> {
9396
let data = data.as_bytes();
9497
let alphabet = match alphabet {

lightning/src/util/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ pub mod invoice;
2222
pub mod persist;
2323
pub mod string;
2424
pub mod wakers;
25+
#[cfg(fuzzing)]
26+
pub mod base32;
27+
/// base32 utils
28+
#[cfg(not(fuzzing))]
2529
pub mod base32;
2630

2731
pub(crate) mod atomic_counter;

0 commit comments

Comments
 (0)