summaryrefslogtreecommitdiff
path: root/debian/patches/22_fix_qiconvcodec.diff
blob: 42dad6ac4510ffca8662f580354f069057543f87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
--- a/src/corelib/codecs/qiconvcodec.cpp
+++ b/src/corelib/codecs/qiconvcodec.cpp
@@ -83,6 +83,8 @@ static Ptr_iconv_close ptr_iconv_close =
 
 QT_BEGIN_NAMESPACE
 
+extern bool qt_locale_initialized;
+
 QIconvCodec::QIconvCodec()
     : utf16Codec(0)
 {
@@ -182,8 +184,9 @@ QString QIconvCodec::convertToUnicode(co
         }
     } else {
         QThreadStorage<QIconvCodec::IconvState *> *ts = toUnicodeState();
-        if (!ts) {
+        if (!qt_locale_initialized || !ts) {
             // we're running after the Q_GLOBAL_STATIC has been deleted
+            // or before the QCoreApplication initialization
             // bad programmer, no cookie for you
             return QString::fromLatin1(chars, len);
         }
@@ -305,9 +308,14 @@ QByteArray QIconvCodec::convertFromUnico
 #endif
 
     QThreadStorage<QIconvCodec::IconvState *> *ts = fromUnicodeState();
-    if (!ts) {
+    if (!qt_locale_initialized || !ts) {
         // we're running after the Q_GLOBAL_STATIC has been deleted
+        // or before the QCoreApplication initialization
         // bad programmer, no cookie for you
+        if (!len)
+            // this is a special case - zero-sized string should be
+            // translated to empty but not-null QByteArray.
+            return QByteArray("");
         return QString::fromRawData(uc, len).toLatin1();
     }
     IconvState *&state = ts->localData();
@@ -384,8 +392,8 @@ QByteArray QIconvCodec::convertFromUnico
                 // fall through
             case EINVAL:
                 {
-                    ++inBytes;
-                    --inBytesLeft;
+                    inBytes += sizeof(QChar);
+                    inBytesLeft -= sizeof(QChar);
                     break;
                 }
             case E2BIG:
--- a/src/corelib/kernel/qcoreapplication.cpp
+++ b/src/corelib/kernel/qcoreapplication.cpp
@@ -163,6 +163,8 @@ void Q_CORE_EXPORT qt_call_post_routines
 bool QCoreApplicationPrivate::is_app_running = false;
  // app closing down if true
 bool QCoreApplicationPrivate::is_app_closing = false;
+// initialized in qcoreapplication and in qtextstream autotest when setlocale is called.
+Q_AUTOTEST_EXPORT bool qt_locale_initialized = false;
 
 
 Q_CORE_EXPORT uint qGlobalPostedEventsCount()
@@ -440,6 +442,7 @@ void QCoreApplication::init()
 
 #ifdef Q_OS_UNIX
     setlocale(LC_ALL, "");                // use correct char set mapping
+    qt_locale_initialized = true;
 #endif
 
 #ifdef Q_WS_WIN