Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-90248

QFile crashes when overwriting file with binary data in file with .txt extension



    • Type: Bug
    • Status: Closed
    • Priority: Not Evaluated
    • Resolution: Out of scope
    • Affects Version/s: 5.12.10
    • Fix Version/s: None
    • Component/s: Core: I/O
    • Labels:
    • Platform/s:


      I had unexplained crashes, I finally was able to extract the code from my project and reduce it to the code below.

      It seems that QFile crashes if binary data is written to a file that ends with the .txt extension even though QIODevice::Text is not specified. Also not on the first file, it seems like changing filenames is needed too.

      Granted, typically giving a binary file an extension .txt is not that logical. But I do not expect it to crash due to the filename used, nor other behavior depending on the extension. 

      I tested against. Qt5.5.1 MinGW, Qt5.12.1 VS2017 32bit, Qt5.12.10 VS2017 32bit and Qt5.12.10 MinGW 64bit. All same behavior. I'm running Windows 10.

      I also replaced the QFile write with basic fopen/fwrite/fclose, and then it does not crash.
      I also tried the setTextMode(false) on the file with no effect. Also tried to use a DataStream in to write, also crashes.
      I was not able to catch it in the debugger by breaking on exception. It crashes without a usable stack trace. 

      Running the following code once, works.
      After the output txt files are created, consecutive runs crash after a few for-loop iterations. It does not trace the "Done", so crashes either in the write or the close or something

      It no longer crashes if (see also the commented code below):
      -Changing the extension to anything but .txt
      -deleting the file at forehand
      -Not changing the destination filename each iteration
      -Using ASCII values, e.g. instead of append (1) appending 65 ('A')

       So to reproduce, run following console app, this goes oke and creates 100 out text files.
      Then run it again. Now it crashes still managing to write a handful of out text files though. The amout of files it manages to write before it crashes also varies, why that is  I have no clue.


      #include <QCoreApplication>
      #include <QFile>
      #include <QDebug>
      int main(int argc, char *argv[])
          QCoreApplication a(argc, argv);
          QByteArray binaryData;
          binaryData.append(1);  // put some non-ASCII char into array
          //binaryData.append(65); // put some ASCII char into array does not crash
          for (int i = 0; i < 100; i++) {
              // using another extension that .txt does NOT crash
              QString outFile = QString("out%1.txt").arg(i);      
              //QString outFile = "out.txt";      // Using a fixed name does not crash
              qDebug() << "Handle" << outFile << "i =" << i;
              QFile out(outFile);
              //out.remove(); // Removing it first does NOT crash
              if (out.open(QIODevice::WriteOnly)) {
                  qDebug() << "Done";
          return 0;
          //return a.exec();





        No reviews matched the request. Check your Options in the drop-down menu of this sections header.



            thiago Thiago Macieira
            dewonderman Michel van Wonderen
            0 Vote for this issue
            4 Start watching this issue



                Gerrit Reviews

                There are no open Gerrit changes