From 237e73df945ad9a82a658647a28706d04b63a6a6 Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Wed, 6 May 2015 22:24:46 +0400 Subject: Don't assume QLocale::codecForLocale always returns non-null It may return null during program exit, due to QCoreGlobalData global static already having been destroyed, or due to the codec name/mib being unsupported by ICU. If that's the case, QTextStream needs to fall back to Latin 1, like QString::toLocal8Bit and fromLocal8Bit already do. Change-Id: Ia888243669e051e78e0dbe0bb1bc55a1c4f519d8 Reviewed-by: Olivier Goffart (Woboq GmbH) Reviewed-by: Oswald Buddenhagen --- src/corelib/io/qtextstream.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/io/qtextstream.cpp b/src/corelib/io/qtextstream.cpp index 5fe4cfef9d..47b96d708f 100644 --- a/src/corelib/io/qtextstream.cpp +++ b/src/corelib/io/qtextstream.cpp @@ -464,7 +464,7 @@ bool QTextStreamPrivate::fillReadBuffer(qint64 maxBytes) } #if defined (QTEXTSTREAM_DEBUG) qDebug("QTextStreamPrivate::fillReadBuffer(), using %s codec", - codec->name().constData()); + codec ? codec->name().constData() : "no"); #endif #endif @@ -476,9 +476,10 @@ bool QTextStreamPrivate::fillReadBuffer(qint64 maxBytes) int oldReadBufferSize = readBuffer.size(); #ifndef QT_NO_TEXTCODEC // convert to unicode - readBuffer += codec->toUnicode(buf, bytesRead, &readConverterState); + readBuffer += Q_LIKELY(codec) ? codec->toUnicode(buf, bytesRead, &readConverterState) + : QString::fromLatin1(buf, bytesRead); #else - readBuffer += QString::fromLatin1(QByteArray(buf, bytesRead).constData()); + readBuffer += QString::fromLatin1(buf, bytesRead); #endif // reset the Text flag. @@ -564,7 +565,8 @@ void QTextStreamPrivate::flushWriteBuffer() codec = QTextCodec::codecForLocale(); #if defined (QTEXTSTREAM_DEBUG) qDebug("QTextStreamPrivate::flushWriteBuffer(), using %s codec (%s generating BOM)", - codec->name().constData(), writeConverterState.flags & QTextCodec::IgnoreHeader ? "not" : ""); + codec ? codec->name().constData() : "no", + !codec || (writeConverterState.flags & QTextCodec::IgnoreHeader) ? "not" : ""); #endif // convert from unicode to raw data @@ -572,7 +574,7 @@ void QTextStreamPrivate::flushWriteBuffer() QByteArray data = Q_LIKELY(codec) ? codec->fromUnicode(writeBuffer.data(), writeBuffer.size(), &writeConverterState) : writeBuffer.toLatin1(); #else - QByteArray data = writeBuffer.toLocal8Bit(); + QByteArray data = writeBuffer.toLatin1(); #endif writeBuffer.clear(); -- cgit v1.2.3