Ver código fonte

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.

darkfi 1 ano atrás
pai
commit
14cae83e1b
1 arquivos alterados com 5 adições e 2 exclusões
  1. 5 2
      src/net/transport/mod.rs

+ 5 - 2
src/net/transport/mod.rs

@@ -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