Author: cazfi
Date: Wed Sep 16 05:32:13 2015
New Revision: 29901

URL: http://svn.gna.org/viewcvs/freeciv?rev=29901&view=rev
Log:
Added mutex protection for the usage of string comparison buffers.

See patch #6349

Modified:
    trunk/common/fc_interface.c
    trunk/utility/support.c
    trunk/utility/support.h

Modified: trunk/common/fc_interface.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/fc_interface.c?rev=29901&r1=29900&r2=29901&view=diff
==============================================================================
--- trunk/common/fc_interface.c (original)
+++ trunk/common/fc_interface.c Wed Sep 16 05:32:13 2015
@@ -61,6 +61,8 @@
 
   fc_funcs_defined = TRUE;
 
+  fc_strAPI_init();
+
   setup_real_activities_array();
 }
 
@@ -75,5 +77,5 @@
   free_freeciv_home_dir();
   free_user_home_dir();
   free_fileinfo_data();
-  cmp_buffers_free();
+  fc_strAPI_free();
 }

Modified: trunk/utility/support.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/utility/support.c?rev=29901&r1=29900&r2=29901&view=diff
==============================================================================
--- trunk/utility/support.c     (original)
+++ trunk/utility/support.c     Wed Sep 16 05:32:13 2015
@@ -111,6 +111,7 @@
 static int cmp_buffer_uchars = 0;
 static UChar *cmp_buffer0 = NULL;
 static UChar *cmp_buffer1 = NULL;
+fc_mutex cmp_buffer_mutex;
 
 /***************************************************************
   Initial allocation of string comparison buffers.
@@ -143,9 +144,20 @@
 }
 
 /***************************************************************
-  Free string comparison buffers.
+  Initialize string handling API
 ***************************************************************/
-void cmp_buffers_free(void)
+void fc_strAPI_init(void)
+{
+  if (cmp_buffer_uchars == 0) {
+    fc_init_mutex(&cmp_buffer_mutex);
+    cmp_buffers_initial();
+  }
+}
+
+/***************************************************************
+  Free string handling API resources
+***************************************************************/
+void fc_strAPI_free(void)
 {
   if (cmp_buffer0 != NULL) {
     free(cmp_buffer0);
@@ -154,6 +166,7 @@
     cmp_buffer1 = NULL;
     cmp_buffer_uchars = 0;
   }
+  fc_destroy_mutex(&cmp_buffer_mutex);
 }
 
 /***************************************************************
@@ -165,6 +178,7 @@
   int len0;
   int len1;
   bool enough_mem = FALSE;
+  int ret;
 
   if (str0 == NULL) {
     return -1;
@@ -173,7 +187,11 @@
     return 1;
   }
 
-  cmp_buffers_initial();
+  if (cmp_buffer_uchars == 0) {
+    fc_strAPI_init();
+  }
+
+  fc_allocate_mutex(&cmp_buffer_mutex);
 
   while (!enough_mem) {
     UErrorCode err_code0 = U_ZERO_ERROR;
@@ -191,8 +209,12 @@
     }
   }
 
-  return u_strCaseCompare(cmp_buffer0, -1, cmp_buffer1, -1,
-                          0, &err_code);
+  ret = u_strCaseCompare(cmp_buffer0, -1, cmp_buffer1, -1,
+                         0, &err_code);
+
+  fc_release_mutex(&cmp_buffer_mutex);
+
+  return ret;
 }
 
 /***************************************************************
@@ -205,6 +227,7 @@
   int len0;
   int len1;
   bool enough_mem = FALSE;
+  int ret;
 
   if (str0 == NULL) {
     return -1;
@@ -213,7 +236,11 @@
     return 1;
   }
 
-  cmp_buffers_initial();
+  if (cmp_buffer_uchars == 0) {
+    fc_strAPI_init();
+  }
+
+  fc_allocate_mutex(&cmp_buffer_mutex);
 
   while (!enough_mem) {
     UErrorCode err_code0 = U_ZERO_ERROR;
@@ -238,8 +265,12 @@
     len1 = n;
   }
 
-  return u_strCaseCompare(cmp_buffer0, len0, cmp_buffer1, len1,
-                          0, &err_code);
+  ret = u_strCaseCompare(cmp_buffer0, len0, cmp_buffer1, len1,
+                         0, &err_code);
+
+  fc_release_mutex(&cmp_buffer_mutex);
+
+  return ret;
 }
 
 /***************************************************************

Modified: trunk/utility/support.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/utility/support.h?rev=29901&r1=29900&r2=29901&view=diff
==============================================================================
--- trunk/utility/support.h     (original)
+++ trunk/utility/support.h     Wed Sep 16 05:32:13 2015
@@ -110,7 +110,8 @@
 int fc_strncasecmp(const char *str0, const char *str1, size_t n);
 int fc_strncasequotecmp(const char *str0, const char *str1, size_t n);
 
-void cmp_buffers_free(void);
+void fc_strAPI_init(void);
+void fc_strAPI_free(void);
 
 size_t effectivestrlenquote(const char *str);
 


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to