Skip to content

Commit 9266631

Browse files
committed
better error handling
1 parent b962020 commit 9266631

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
@@ -660,26 +660,44 @@ impl FromStr for NetAddress {
660660
let port: u16 = addr.port();
661661
match addr {
662662
SocketAddr::V4(addr) => {
663-
let addr = addr.ip().to_string().parse::<Ipv4Addr>()?;
664-
return Ok(NetAddress::IPv4 { addr: addr.octets(), port });
663+
let addr = addr.ip().octets();
664+
return Ok(NetAddress::IPv4 { addr, port });
665665
},
666666
SocketAddr::V6(addr) => {
667-
let addr = addr.ip().to_string().parse::<Ipv6Addr>()?;
668-
return Ok(NetAddress::IPv6 { addr: addr.octets(), port });
667+
let addr = addr.ip().octets();
668+
return Ok(NetAddress::IPv6 { addr, port });
669669
},
670670
}
671671
},
672672
Err(e) => {
673-
let trimmed_input = s.rfind(":").expect("Invalid input, needs to include seperator \":\"");
673+
let trimmed_input = match s.rfind(":") {
674+
Some(pos) => pos,
675+
None => return Err(e),
676+
};
674677
let host = &s[..trimmed_input];
675-
let port: u16 = s[trimmed_input + 1..].parse().expect("Invalid input, port needs to be u16");
678+
let port: u16 = match s[trimmed_input + 1..].parse() {
679+
Ok(port) => port,
680+
Err(_) => return Err(e),
681+
};
676682
if host.ends_with(".onion") {
677-
let onion = host.split(".onion").collect::<Vec<&str>>()[0];
678-
let onion = base32::decode(base32::Alphabet::RFC4648 { padding: false }, &onion).expect("Error decoding onion address");
679-
let version = onion[0];
683+
let onion = match host.split(".onion").nth(0) {
684+
Some(onion) => onion,
685+
None => return Err(e),
686+
};
687+
let onion = match base32::decode(base32::Alphabet::RFC4648 { padding: false }, &onion) {
688+
Some(onion) => onion,
689+
None => return Err(e),
690+
};
691+
let version = match onion.get(0) {
692+
Some(version) => version,
693+
None => return Err(e),
694+
};
680695
let checksum = u16::from_be_bytes([onion[1], onion[2]]);
681-
let ed25519_pubkey = onion[3..35].try_into().expect("Invalid onion address");
682-
return Ok(NetAddress::OnionV3 { ed25519_pubkey, checksum, version, port });
696+
let ed25519_pubkey = match onion[3..35].try_into() {
697+
Ok(ed25519_pubkey) => ed25519_pubkey,
698+
Err(_) => return Err(e),
699+
};
700+
return Ok(NetAddress::OnionV3 { ed25519_pubkey, checksum, version: *version, port });
683701
}
684702

685703
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)