Date: Wednesday, January 3, 2007 @ 14:17:49
  Author: csaba
    Path: /cvsroot/carob/carob/src

Modified: StringCodecs.cpp (1.12 -> 1.13)

Since the conversion descriptor returned by iconv_open() is stateful, the 
IconvCodec is not thread safe. Added locking and reinitializing the iconv state.


------------------+
 StringCodecs.cpp |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletion(-)


Index: carob/src/StringCodecs.cpp
diff -u carob/src/StringCodecs.cpp:1.12 carob/src/StringCodecs.cpp:1.13
--- carob/src/StringCodecs.cpp:1.12     Wed Dec 20 18:23:23 2006
+++ carob/src/StringCodecs.cpp  Wed Jan  3 14:17:49 2007
@@ -168,6 +168,10 @@
 
 #include <errno.h>
 
+#include <CriticalSection.hpp>
+
+static CriticalSection iconvCodecCS;
+
 IconvCodec::IconvCodec() : cd_from(iconv_t(-1)), cd_to(iconv_t(-1))
 {
 }
@@ -214,11 +218,17 @@
   if (s.empty())
     return std::wstring();
 
+  LockScope ls(&iconvCodecCS);
+   
   std::wstring result;
 
   const char *inbuf = s.data();
   size_t inbytesleft = s.length();
-  
+
+  // reset the state
+  if (iconv_const_adapter(iconv, cd_to, 0, 0, 0, 0) == (size_t) -1)
+    throw CodecException(L"iconv reset failed.");
+     
   wchar_t convertbuf[CONVERTER_BUFFER_SIZE];
   char *outbuf;
   size_t outbytesleft;
@@ -256,6 +266,8 @@
   if (w.empty())
     return std::string();
   
+  LockScope ls(&iconvCodecCS);
+  
   std::string result;
 
   const char *inbuf = reinterpret_cast<const char *>(w.data());
@@ -265,6 +277,10 @@
   char *outbuf;
   size_t outbytesleft;
   
+  // reset the state
+  if (iconv_const_adapter(iconv, cd_from, 0, 0, 0, 0) == (size_t) -1)
+    throw CodecException(L"iconv reset failed.");
+
   while (inbytesleft > 0)
   {
     outbuf = convertbuf;

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to