Details
-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
5.14
-
None
Description
The following code will end up into an endless while loop:
#include <QtCore> int main(int argc, char *argv[]) { const char fileWithCRLF[] = "c:\\temp\\fileWithCRLF.txt"; { // Generating text file QFile textFile(fileWithCRLF); textFile.open(QIODevice::WriteOnly); textFile.write("Qt\u000d\u000a\0"); // "Qt<CR+LF>" } FILE *stdFile = fopen(fileWithCRLF, "r"); // Implicitly opens in text mode, filters out CR QTextStream is(stdFile); while (!is.atEnd()) // atEnd() eternally returns false puts(qPrintable(is.readLine())); return 0; }
A FILE handle to a text file containing CR+LF newlines is opened. The implicit open mode is by default "t". That text mode causes CR (0x0d) bytes of CR+LF pairs to be filtered out. Therefore, out of the originally 4 bytes in the file, only 3 are read and later processed by QTextStream.
QTextStream::atEnd() returns false if the current stream position in the buffer (3 bytes) is smaller than the size of the original file from the file system (4 bytes).
The comparison of buffer position vs. file size happens in QIODevice::bytesAvailable().
A real life case of this admittetly unusual scenario is in qmake's sed replacement: https://code.woboq.org/qt5/qtbase/qmake/main.cpp.html#153
Attachments
Issue Links
- is required for
-
QTBUG-80443 "qmake -install sed" fails on input with CR+LF (0x0D+0x0A) newlines
-
- Closed
-