net: fix unwrap() crash where listener.endpoint() is called before listener.listen(). Technically should not be done, but API should not crash in this case.

This commit is contained in:
darkfi
2025-02-08 14:00:43 +01:00
parent 243c122bcb
commit 14cae83e1b

View File

@@ -370,6 +370,7 @@ impl Listener {
}
}
/// Should only be called after `listen()` in order to behave correctly.
pub async fn endpoint(&self) -> Url {
match &self.variant {
ListenerVariant::Tcp(listener) | ListenerVariant::TcpTls(listener) => {
@@ -382,8 +383,10 @@ impl Listener {
// `port == 0` means we got the OS to assign a random listen port to us.
// Get the port from the listener and modify the endpoint.
if port == 0 {
let actual_port = *listener.port.get().unwrap();
endpoint.set_port(Some(actual_port)).unwrap();
// Was `.listen()` called yet? Otherwise do nothing
if let Some(actual_port) = listener.port.get() {
endpoint.set_port(Some(*actual_port)).unwrap();
}
}
endpoint