Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2004:
[Freeciv-Dev] (PR#10994) auth fail and disconnect causes a segfault
Home

[Freeciv-Dev] (PR#10994) auth fail and disconnect causes a segfault

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: kaufman@xxxxxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] (PR#10994) auth fail and disconnect causes a segfault
From: "Vasco Alexandre da Silva Costa" <vasc@xxxxxxxxxxxxxx>
Date: Fri, 12 Nov 2004 19:15:04 -0800
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=10994 >

This patch fixes connection_common_close() to be more verbose and less
randomly suicidal on erroneous orders.

Index: common/connection.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/connection.c,v
retrieving revision 1.43
diff -u -r1.43 connection.c
--- common/connection.c 9 Sep 2004 17:23:41 -0000       1.43
+++ common/connection.c 13 Nov 2004 03:13:39 -0000
@@ -570,22 +570,32 @@
 
   conn_clear_packet_cache(pc);
 
-  for (i = 0; i < PACKET_LAST; i++) {
-    if (pc->phs.sent[i] != NULL) {
-      hash_free(pc->phs.sent[i]);
-      pc->phs.sent[i] = NULL;
+  if (pc->phs.sent) {
+    for (i = 0; i < PACKET_LAST; i++) {
+      if (pc->phs.sent[i] != NULL) {
+       hash_free(pc->phs.sent[i]);
+       pc->phs.sent[i] = NULL;
+      }
     }
-    if (pc->phs.received[i] != NULL) {
-      hash_free(pc->phs.received[i]);
-      pc->phs.received[i] = NULL;
+    free(pc->phs.sent);
+    pc->phs.sent = NULL;
+  }
+
+  if (pc->phs.received) {
+    for (i = 0; i < PACKET_LAST; i++) {
+      if (pc->phs.received[i] != NULL) {
+       hash_free(pc->phs.received[i]);
+       pc->phs.received[i] = NULL;
+      }
     }
+    free(pc->phs.received);
+    pc->phs.received = NULL;
+  }
+
+  if (pc->phs.variant) {
+    free(pc->phs.variant);
+    pc->phs.variant = NULL;
   }
-  free(pc->phs.sent);
-  pc->phs.sent = NULL;
-  free(pc->phs.received);
-  pc->phs.received = NULL;
-  free(pc->phs.variant);
-  pc->phs.variant = NULL;
 }
 
 /**************************************************************************
@@ -612,18 +622,22 @@
 **************************************************************************/
 void connection_common_close(struct connection *pconn)
 {
-  my_closesocket(pconn->sock);
-  pconn->used = FALSE;
-  pconn->established = FALSE;
+  if (!pconn->used) {
+    freelog(LOG_ERROR, "WARNING: Trying to close already closed connection");
+  } else {
+    my_closesocket(pconn->sock);
+    pconn->used = FALSE;
+    pconn->established = FALSE;
 
-  free_socket_packet_buffer(pconn->buffer);
-  pconn->buffer = NULL;
+    free_socket_packet_buffer(pconn->buffer);
+    pconn->buffer = NULL;
 
-  free_socket_packet_buffer(pconn->send_buffer);
-  pconn->send_buffer = NULL;
+    free_socket_packet_buffer(pconn->send_buffer);
+    pconn->send_buffer = NULL;
 
-  free_compression_queue(pconn);
-  free_packet_hashes(pconn);
+    free_compression_queue(pconn);
+    free_packet_hashes(pconn);
+  }
 }
 
 /**************************************************************************
@@ -635,7 +649,7 @@
   int i;
 
   for (i = 0; i < PACKET_LAST; i++) {
-    if (pc->phs.sent[i] != NULL) {
+    if (pc->phs.sent != NULL && pc->phs.sent[i] != NULL) {
       struct hash_table *hash = pc->phs.sent[i];
       while (hash_num_entries(hash) > 0) {
        const void *key = hash_key_by_number(hash, 0);
@@ -643,7 +657,7 @@
        free((void *) key);
       }
     }
-    if (pc->phs.received[i] != NULL) {
+    if (pc->phs.received != NULL && pc->phs.received[i] != NULL) {
       struct hash_table *hash = pc->phs.received[i];
       while (hash_num_entries(hash) > 0) {
        const void *key = hash_key_by_number(hash, 0);

[Prev in Thread] Current Thread [Next in Thread]