From bb118168f4465ea81396ae6bbf2c11baaba6539e Mon Sep 17 00:00:00 2001
From: Stas'M <x86corez@gmail.com>
Date: Sat, 14 Mar 2020 22:50:40 +0300
Subject: [PATCH] Net: Add sanity checks. Forward ports only after host is
 created.

Addendum to 10f31fd.
---
 src/game/g_net.pas | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/game/g_net.pas b/src/game/g_net.pas
index 07d9713..7dad24c 100644
--- a/src/game/g_net.pas
+++ b/src/game/g_net.pas
@@ -1492,8 +1492,6 @@ begin
   NetAddr.host := IPAddr;
   NetAddr.port := Port;
 
-  if NetForwardPorts then NetPortThread := BeginThread(ForwardThread);
-
   NetHost := enet_host_create(@NetAddr, NET_MAXCLIENTS, NET_CHANS, 0, 0);
 
   if (NetHost = nil) then
@@ -1504,6 +1502,8 @@ begin
     Exit;
   end;
 
+  if NetForwardPorts then NetPortThread := BeginThread(ForwardThread);
+
   NetPongSock := enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
   if NetPongSock <> ENET_SOCKET_NULL then
   begin
@@ -1606,7 +1606,7 @@ var
   Ping: array [0..9] of Byte;
   NPl: Byte;
 begin
-  if NetPongSock = ENET_SOCKET_NULL then Exit;
+  if (NetPongSock = ENET_SOCKET_NULL) or (NetHost = nil) then Exit;
 
   Buf.data := Addr(Ping[0]);
   Buf.dataLength := 2+8;
@@ -2238,6 +2238,9 @@ var
 begin
   Result := False;
 
+  if NetHost = nil then
+    exit;
+
   if NetPortForwarded = NetHost.address.port then
   begin
     Result := True;
-- 
2.29.2