<URL: http://bugs.freeciv.org/Ticket/Display.html?id=40028 >

Here is a quick and partial fix.  I assume that strerror() is one of the
most common offending functions, so I quickly went through and converted
all mystrerror users in client/ and server/ directories to use the
newly-written L_().  The only real issue with it is the use of
fixed-sized buffers which are wasteful if too large and broken if too
small, but as I said in my previous message I see no real alternative here.

Madeline, if you can test this and verify it fixes those strings then at
least we'll know we're getting somewhere.  It would also be helpful if
you'd point out to us what other strings are being handled wrongly.

It is important to recognize which strings go in which encoding. If you
use printf() directly your strings must be in the local encoding.  More
commonly freelog or fc_fprintf is used and these functions expect
strings in the internal encoding.  Generally all freeciv functions
should expect the internal encoding unless specifically documented
otherwise.

Also as a side note there's another library we use, lua.  Strings from
LUA are probably in the local encoding so should use L_ too.

-jason

Index: utility/fciconv.h
===================================================================
--- utility/fciconv.h	(revision 14336)
+++ utility/fciconv.h	(working copy)
@@ -19,6 +19,8 @@
 
 #define FC_DEFAULT_DATA_ENCODING "UTF-8"
 
+#define L_(s, b) local_to_internal_string_buffer((s), (b), sizeof(b))
+
 void init_character_encodings(const char *internal_encoding,
 			      bool use_transliteration);
 
Index: server/meta.c
===================================================================
--- server/meta.c	(revision 14336)
+++ server/meta.c	(working copy)
@@ -44,6 +44,7 @@
 #include "connection.h"
 #include "dataio.h"
 #include "fcintl.h"
+#include "fciconv.h"
 #include "log.h"
 #include "mem.h"
 #include "netintf.h"
@@ -200,6 +201,7 @@
 {
   static char msg[8192];
   static char str[8192];
+  char buf[512];
   int rest = sizeof(str);
   int n = 0;
   char *s = str;
@@ -213,13 +215,14 @@
 
   if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
     freelog(LOG_ERROR, "Metaserver: can't open stream socket: %s",
-	    mystrerror());
+	    L_(mystrerror(), buf));
     metaserver_failed();
     return FALSE;
   }
 
   if (my_connect(sock, (struct sockaddr *) &meta_addr, sizeof(meta_addr)) == -1) {
-    freelog(LOG_ERROR, "Metaserver: connect failed: %s", mystrerror());
+    freelog(LOG_ERROR, "Metaserver: connect failed: %s",
+	    L_(mystrerror(), buf));
     metaserver_failed();
     my_closesocket(sock);
     return FALSE;
Index: server/sernet.c
===================================================================
--- server/sernet.c	(revision 14336)
+++ server/sernet.c	(working copy)
@@ -806,7 +806,9 @@
   fromlen = sizeof(fromend);
 
   if ((new_sock = accept(sockfd, &fromend.sockaddr, &fromlen)) == -1) {
-    freelog(LOG_ERROR, "accept failed: %s", mystrerror());
+    char buf[512];
+
+    freelog(LOG_ERROR, "accept failed: %s", L_(mystrerror(), buf));
     return -1;
   }
 
@@ -885,16 +887,17 @@
   struct ip_mreq mreq;
   const char *group;
   int opt;
+  char buf[512];
 
   /* Create socket for client connections. */
   if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
-    die("socket failed: %s", mystrerror());
+    die("socket failed: %s", L_(mystrerror(), buf));
   }
 
   opt=1; 
   if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 
 		(char *)&opt, sizeof(opt)) == -1) {
-    freelog(LOG_ERROR, "SO_REUSEADDR failed: %s", mystrerror());
+    freelog(LOG_ERROR, "SO_REUSEADDR failed: %s", L_(mystrerror(), buf));
   }
 
   if (!net_lookup_service(srvarg.bind_addr, srvarg.port, &src)) {
@@ -904,23 +907,23 @@
   }
 
   if(bind(sock, &src.sockaddr, sizeof (src)) == -1) {
-    freelog(LOG_FATAL, "bind failed: %s", mystrerror());
+    freelog(LOG_FATAL, "bind failed: %s", L_(mystrerror(), buf));
     exit(EXIT_FAILURE);
   }
 
   if(listen(sock, MAX_NUM_CONNECTIONS) == -1) {
-    freelog(LOG_FATAL, "listen failed: %s", mystrerror());
+    freelog(LOG_FATAL, "listen failed: %s", L_(mystrerror(), buf));
     exit(EXIT_FAILURE);
   }
 
   /* Create socket for server LAN announcements */
   if ((socklan = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-     freelog(LOG_ERROR, "socket failed: %s", mystrerror());
+    freelog(LOG_ERROR, "socket failed: %s", L_(mystrerror(), buf));
   }
 
   if (setsockopt(socklan, SOL_SOCKET, SO_REUSEADDR,
                  (char *)&opt, sizeof(opt)) == -1) {
-    freelog(LOG_ERROR, "SO_REUSEADDR failed: %s", mystrerror());
+    freelog(LOG_ERROR, "SO_REUSEADDR failed: %s", L_(mystrerror(), buf));
   }
 
   my_nonblock(socklan);
@@ -933,7 +936,7 @@
   addr.sockaddr_in.sin_port = htons(SERVER_LAN_PORT);
 
   if (bind(socklan, &addr.sockaddr, sizeof(addr)) < 0) {
-    freelog(LOG_ERROR, "bind failed: %s", mystrerror());
+    freelog(LOG_ERROR, "bind failed: %s", L_(mystrerror(), buf));
   }
 
   mreq.imr_multiaddr.s_addr = inet_addr(group);
@@ -941,7 +944,7 @@
 
   if (setsockopt(socklan, IPPROTO_IP, IP_ADD_MEMBERSHIP,
                  (const char*)&mreq, sizeof(mreq)) < 0) {
-    freelog(LOG_ERROR, "setsockopt failed: %s", mystrerror());
+    freelog(LOG_ERROR, "setsockopt failed: %s", L_(mystrerror(), buf));
   }
 
   close_socket_set_callback(close_socket_callback);
@@ -1069,6 +1072,7 @@
   int type;
   fd_set readfs, exceptfs;
   struct timeval tv;
+  char buf[512];
 
   if (with_ggz) {
     return;
@@ -1084,7 +1088,7 @@
 
   while (my_select(socklan + 1, &readfs, NULL, &exceptfs, &tv) == -1) {
     if (errno != EINTR) {
-      freelog(LOG_ERROR, "select failed: %s", mystrerror());
+      freelog(LOG_ERROR, "select failed: %s", L_(mystrerror(), buf));
       return;
     }
     /* EINTR can happen sometimes, especially when compiling with -pg.
@@ -1126,10 +1130,11 @@
 #ifndef HAVE_WINSOCK
   unsigned char ttl;
 #endif
+  char buf[512];
 
   /* Create a socket to broadcast to client. */
   if ((socksend = socket(AF_INET,SOCK_DGRAM, 0)) < 0) {
-    freelog(LOG_ERROR, "socket failed: %s", mystrerror());
+    freelog(LOG_ERROR, "socket failed: %s", L_(mystrerror(), buf));
     return;
   }
 
@@ -1147,14 +1152,14 @@
   ttl = SERVER_LAN_TTL;
   if (setsockopt(socksend, IPPROTO_IP, IP_MULTICAST_TTL, 
                  (const char*)&ttl, sizeof(ttl))) {
-    freelog(LOG_ERROR, "setsockopt failed: %s", mystrerror());
+    freelog(LOG_ERROR, "setsockopt failed: %s", L_(mystrerror(), buf));
     return;
   }
 #endif
 
   if (setsockopt(socksend, SOL_SOCKET, SO_BROADCAST, 
                  (const char*)&setting, sizeof(setting))) {
-    freelog(LOG_ERROR, "setsockopt failed: %s", mystrerror());
+    freelog(LOG_ERROR, "setsockopt failed: %s", L_(mystrerror(), buf));
     return;
   }
 
@@ -1198,7 +1203,7 @@
   /* Sending packet to client with the information gathered above. */
   if (sendto(socksend, buffer,  size, 0, &addr.sockaddr,
       sizeof(addr)) < 0) {
-    freelog(LOG_ERROR, "sendto failed: %s", mystrerror());
+    freelog(LOG_ERROR, "sendto failed: %s", L_(mystrerror(), buf));
     return;
   }
 
Index: client/clinet.c
===================================================================
--- client/clinet.c	(revision 14336)
+++ client/clinet.c	(working copy)
@@ -64,6 +64,7 @@
 
 #include "capstr.h"
 #include "dataio.h"
+#include "fciconv.h"
 #include "fcintl.h"
 #include "game.h"
 #include "hash.h"
@@ -212,13 +213,13 @@
   }
   
   if ((aconnection.sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
-    (void) mystrlcpy(errbuf, mystrerror(), errbufsize);
+    (void) local_to_internal_string_buffer(mystrerror(), errbuf, errbufsize);
     return -1;
   }
 
   if (my_connect(aconnection.sock, &server_addr.sockaddr,
       sizeof(server_addr)) == -1) {
-    (void) mystrlcpy(errbuf, mystrerror(), errbufsize);
+    (void) local_to_internal_string_buffer(mystrerror(), errbuf, errbufsize);
     my_closesocket(aconnection.sock);
     aconnection.sock = -1;
 #ifdef HAVE_WINSOCK
@@ -309,6 +310,7 @@
 				&& pc->send_buffer->ndata > 0);
     int n;
     struct timeval tv;
+    char buf[512];
 
     tv.tv_sec = 0;
     tv.tv_usec = 0;
@@ -346,7 +348,7 @@
       }
 
       freelog(LOG_NORMAL, "error in select() return=%d errno=%d (%s)",
-	      n, errno, mystrerror());
+	      n, errno, L_(mystrerror(), buf));
       return -1;
     }
 
Index: client/servers.c
===================================================================
--- client/servers.c	(revision 14336)
+++ client/servers.c	(working copy)
@@ -56,6 +56,7 @@
 #endif
 
 #include "fcintl.h"
+#include "fciconv.h"
 #include "hash.h"
 #include "log.h"
 #include "mem.h"
@@ -313,8 +314,10 @@
     capstr);
 
   if (my_writesocket(scan->sock, str, strlen(str)) != strlen(str)) {
+    char buf[512];
+
     /* Even with non-blocking this shouldn't fail. */
-    (scan->error_func)(scan, mystrerror());
+    (scan->error_func)(scan, L_(mystrerror(), buf));
     return;
   }
 
@@ -350,11 +353,13 @@
     result = my_readsocket(scan->sock, buf, sizeof(buf));
 
     if (result < 0) {
+      char buf[512];
+
       if (errno == EAGAIN || errno == EINTR) {
 	/* Keep waiting. */
 	return;
       }
-      (scan->error_func)(scan, mystrerror());
+      (scan->error_func)(scan, L_(mystrerror(), buf));
       return;
     } else if (result == 0) {
       fz_FILE *f;
@@ -381,7 +386,9 @@
       return;
     } else {
       if (fwrite(buf, 1, result, scan->meta.fp) != result) {
-	(scan->error_func)(scan, mystrerror());
+	char buf[512];
+
+	(scan->error_func)(scan, L_(mystrerror(), buf));
       }
     }
   }
@@ -415,7 +422,9 @@
   }
   
   if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
-    (scan->error_func)(scan, mystrerror());
+    char buf[512];
+
+    (scan->error_func)(scan, L_(mystrerror(), buf));
     return FALSE;
   }
 
@@ -427,8 +436,10 @@
       scan->meta.state = META_CONNECTING;
       scan->sock = s;
     } else {
+      char buf[512];
+
       my_closesocket(s);
-      (scan->error_func)(scan, mystrerror());
+      (scan->error_func)(scan, L_(mystrerror(), buf));
       return FALSE;
     }
   } else {
@@ -461,7 +472,9 @@
   switch (scan->meta.state) {
   case META_CONNECTING:
     if (my_select(scan->sock + 1, NULL, &sockset, NULL, &tv) < 0) {
-      (scan->error_func)(scan, mystrerror());
+      char buf[512];
+
+      (scan->error_func)(scan, L_(mystrerror(), buf));
     } else if (FD_ISSET(scan->sock, &sockset)) {
       meta_send_request(scan);
     } else {
@@ -470,7 +483,9 @@
     return NULL;
   case META_WAITING:
     if (my_select(scan->sock + 1, &sockset, NULL, NULL, &tv) < 0) {
-      (scan->error_func)(scan, mystrerror());
+      char buf[512];
+
+      (scan->error_func)(scan, L_(mystrerror(), buf));
     } else if (FD_ISSET(scan->sock, &sockset)) {
       meta_read_response(scan);
       return scan->servers;
@@ -543,13 +558,17 @@
 
   /* Create a socket for broadcasting to servers. */
   if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-    freelog(LOG_ERROR, "socket failed: %s", mystrerror());
+    char buf[512];
+
+    freelog(LOG_ERROR, "socket failed: %s", L_(mystrerror(), buf));
     return FALSE;
   }
 
   if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
                  (char *)&opt, sizeof(opt)) == -1) {
-    freelog(LOG_ERROR, "SO_REUSEADDR failed: %s", mystrerror());
+    char buf[512];
+
+    freelog(LOG_ERROR, "SO_REUSEADDR failed: %s", L_(mystrerror(), buf));
   }
 
   /* Set the UDP Multicast group IP address. */
@@ -566,14 +585,18 @@
   ttl = SERVER_LAN_TTL;
   if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&ttl, 
                  sizeof(ttl))) {
-    freelog(LOG_ERROR, "setsockopt failed: %s", mystrerror());
+    char buf[512];
+
+    freelog(LOG_ERROR, "setsockopt failed: %s", L_(mystrerror(), buf));
     return FALSE;
   }
 #endif
 
   if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char*)&opt, 
                  sizeof(opt))) {
-    freelog(LOG_ERROR, "setsockopt failed: %s", mystrerror());
+    char buf[512];
+
+    freelog(LOG_ERROR, "setsockopt failed: %s", L_(mystrerror(), buf));
     return FALSE;
   }
 
@@ -586,7 +609,9 @@
       sizeof(addr)) < 0) {
     /* This can happen when there's no network connection - it should
      * give an in-game message. */
-    freelog(LOG_ERROR, "sendto failed: %s", mystrerror());
+    char buf[512];
+
+    freelog(LOG_ERROR, "sendto failed: %s", L_(mystrerror(), buf));
     return FALSE;
   } else {
     freelog(LOG_DEBUG, ("Sending request for server announcement on LAN."));
@@ -596,7 +621,9 @@
 
   /* Create a socket for listening for server packets. */
   if ((scan->sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-    (scan->error_func)(scan, mystrerror());
+    char buf[512];
+
+    (scan->error_func)(scan, L_(mystrerror(), buf));
     return FALSE;
   }
 
@@ -604,7 +631,9 @@
 
   if (setsockopt(scan->sock, SOL_SOCKET, SO_REUSEADDR,
                  (char *)&opt, sizeof(opt)) == -1) {
-    freelog(LOG_ERROR, "SO_REUSEADDR failed: %s", mystrerror());
+    char buf[512];
+
+    freelog(LOG_ERROR, "SO_REUSEADDR failed: %s", L_(mystrerror(), buf));
   }
                                                                                
   memset(&addr, 0, sizeof(addr));
@@ -613,7 +642,9 @@
   addr.sockaddr_in.sin_port = htons(SERVER_LAN_PORT + 1);
 
   if (bind(scan->sock, &addr.sockaddr, sizeof(addr)) < 0) {
-    (scan->error_func)(scan, mystrerror());
+    char buf[512];
+
+    (scan->error_func)(scan, L_(mystrerror(), buf));
     return FALSE;
   }
 
@@ -621,7 +652,9 @@
   mreq.imr_interface.s_addr = htonl(INADDR_ANY);
   if (setsockopt(scan->sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, 
                  (const char*)&mreq, sizeof(mreq)) < 0) {
-    (scan->error_func)(scan, mystrerror());
+    char buf[512];
+
+    (scan->error_func)(scan, L_(mystrerror(), buf));
     return FALSE;
   }
 
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to