Skip to content

Commit 471c5d7

Browse files
committed
better error handling
1 parent 75ec6d6 commit 471c5d7

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

lightning/src/ln/msgs.rs

+29-11
Original file line numberDiff line numberDiff line change
@@ -671,26 +671,44 @@ impl FromStr for NetAddress {
671671
let port: u16 = addr.port();
672672
match addr {
673673
SocketAddr::V4(addr) => {
674-
let addr = addr.ip().to_string().parse::<Ipv4Addr>()?;
675-
return Ok(NetAddress::IPv4 { addr: addr.octets(), port });
674+
let addr = addr.ip().octets();
675+
return Ok(NetAddress::IPv4 { addr, port });
676676
},
677677
SocketAddr::V6(addr) => {
678-
let addr = addr.ip().to_string().parse::<Ipv6Addr>()?;
679-
return Ok(NetAddress::IPv6 { addr: addr.octets(), port });
678+
let addr = addr.ip().octets();
679+
return Ok(NetAddress::IPv6 { addr, port });
680680
},
681681
}
682682
},
683683
Err(e) => {
684-
let trimmed_input = s.rfind(":").expect("Invalid input, needs to include seperator \":\"");
684+
let trimmed_input = match s.rfind(":") {
685+
Some(pos) => pos,
686+
None => return Err(e),
687+
};
685688
let host = &s[..trimmed_input];
686-
let port: u16 = s[trimmed_input + 1..].parse().expect("Invalid input, port needs to be u16");
689+
let port: u16 = match s[trimmed_input + 1..].parse() {
690+
Ok(port) => port,
691+
Err(_) => return Err(e),
692+
};
687693
if host.ends_with(".onion") {
688-
let onion = host.split(".onion").collect::<Vec<&str>>()[0];
689-
let onion = base32::decode(base32::Alphabet::RFC4648 { padding: false }, &onion).expect("Error decoding onion address");
690-
let version = onion[0];
694+
let onion = match host.split(".onion").nth(0) {
695+
Some(onion) => onion,
696+
None => return Err(e),
697+
};
698+
let onion = match base32::decode(base32::Alphabet::RFC4648 { padding: false }, &onion) {
699+
Some(onion) => onion,
700+
None => return Err(e),
701+
};
702+
let version = match onion.get(0) {
703+
Some(version) => version,
704+
None => return Err(e),
705+
};
691706
let checksum = u16::from_be_bytes([onion[1], onion[2]]);
692-
let ed25519_pubkey = onion[3..35].try_into().expect("Invalid onion address");
693-
return Ok(NetAddress::OnionV3 { ed25519_pubkey, checksum, version, port });
707+
let ed25519_pubkey = match onion[3..35].try_into() {
708+
Ok(ed25519_pubkey) => ed25519_pubkey,
709+
Err(_) => return Err(e),
710+
};
711+
return Ok(NetAddress::OnionV3 { ed25519_pubkey, checksum, version: *version, port });
694712
}
695713

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

lightning/src/util/base32.rs

+6-3
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

+4
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)