Ensure QTextStream doesn't modify the Text flag on the underlying iodevice

An empty read or a failed write on the underlying QIODevice of the text
stream would lead to an early return where we wouldn't correctly restore
the QIODevice::Text flag of the io device.

Change-Id: I5b632f45dea6ede3f408113556c3dad1b96574e2
Task-number: QTBUG-47176
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
bb10
Lars Knoll 2015-11-27 13:28:45 +01:00
parent f2bd0d1192
commit d0b54cede8
2 changed files with 28 additions and 10 deletions

View File

@ -449,6 +449,10 @@ bool QTextStreamPrivate::fillReadBuffer(qint64 maxBytes)
bytesRead = device->read(buf, sizeof(buf));
}
// reset the Text flag.
if (textModeEnabled)
device->setTextModeEnabled(true);
if (bytesRead <= 0)
return false;
@ -484,10 +488,6 @@ bool QTextStreamPrivate::fillReadBuffer(qint64 maxBytes)
readBuffer += QString::fromLatin1(buf, bytesRead);
#endif
// reset the Text flag.
if (textModeEnabled)
device->setTextModeEnabled(true);
// remove all '\r\n' in the string.
if (readBuffer.size() > oldReadBufferSize && textModeEnabled) {
QChar CR = QLatin1Char('\r');
@ -586,17 +586,18 @@ void QTextStreamPrivate::flushWriteBuffer()
qDebug("QTextStreamPrivate::flushWriteBuffer(), device->write(\"%s\") == %d",
qt_prettyDebug(data.constData(), qMin(data.size(),32), data.size()).constData(), int(bytesWritten));
#endif
#if defined (Q_OS_WIN)
// reset the text flag
if (textModeEnabled)
device->setTextModeEnabled(true);
#endif
if (bytesWritten <= 0) {
status = QTextStream::WriteFailed;
return;
}
#if defined (Q_OS_WIN)
// replace the text flag
if (textModeEnabled)
device->setTextModeEnabled(true);
#endif
// flush the file
#ifndef QT_NO_QOBJECT
QFileDevice *file = qobject_cast<QFileDevice *>(device);

View File

@ -228,6 +228,8 @@ private slots:
void alignAccountingStyle();
void setCodec();
void textModeOnEmptyRead();
private:
void generateLineData(bool for_QString);
void generateAllData(bool for_QString);
@ -3045,6 +3047,21 @@ void tst_QTextStream::int_write_with_locale()
QCOMPARE(result, output);
}
void tst_QTextStream::textModeOnEmptyRead()
{
const QString filename("textmodetest.txt");
QFile::remove(filename); // Remove file if exists
QFile file(filename);
QVERIFY(file.open(QIODevice::ReadWrite | QIODevice::Text));
QTextStream stream(&file);
QVERIFY(file.isTextModeEnabled());
QString emptyLine = stream.readLine(); // Text mode flag cleared here
QVERIFY(file.isTextModeEnabled());
}
// ------------------------------------------------------------------------------
QTEST_MAIN(tst_QTextStream)