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

QFile::atEnd() returns false when it should return true in a specific use case

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P2: Important
    • Resolution: Done
    • Affects Version/s: 4.8.x, 5.8.0 Beta
    • Fix Version/s: 5.9.0 Beta 2
    • Component/s: Core: I/O
    • Labels:
      None
    • Environment:
      linux
      4.8.x
      fedora 22
    • Commits:
      f53ab9d7363e2fb3b63cec1a00851f84f901de18

      Description

      QFile::atEnd() incorrectly returns false under the following conditions

      • open a qfile
      • perform an operation causing the d->cachedSize of QFilePrivate to be set
        such as atEnd(),size(),resize(),bytesAvailable()
      • close file
      • open a different filename that is smaller than the first using the same qfile object
      • d->cachedSize now incorrectly refers to the larger size from the 1st file
      • atEnd() will now always report false for regular files since (pos() < d->cachedSize) is always true. This line always occurs before the last "return bytesAvailable() == 0" line is reached in atEnd(). d->cachedSize would have been reset in the size() call within QIODevice::bytesAvailable() as occured when the 1st file initially called atEnd().
      • the above behavior will occur unless the size(),resize(),bytesAvailable() is called to reset d->cachedSize before the end of file condition is reached.

      Our workaround is to call one of these methods, such as size(), after opening the file for cases where we want to open/close multiple files with the same object, or to just avoid using atEnd() in those situations.

      This would not occur if only one file is open/closed, the 2nd file is larger than the first one, methods such as bytesAvailable() are called before the end of the file, or atEnd() is not needed.

      In the qt code this could be fixed by always setting d->cachedSize = 0 during the QFile::open() call. Currently this is only done in the constructor and a few other cases.

      This bug was surprising but after verifying our specific use case and looking at the qt code, this bug appears to have been there from the very beginning.

        Attachments

        1. biggerfile
          0.0 kB
        2. file16k
          16 kB
        3. filegt16k
          17 kB
        4. main.cpp
          4 kB
        5. main.cpp
          4 kB
        6. qfilebug.pro
          0.1 kB
        7. qfilebug.pro.user
          19 kB
        8. smallfile
          0.0 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            aleksey_lysenko Aleksey Lysenko
            Reporter:
            chrisnichols69 Chris Nichols
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes