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

QDataStream should gracefully handle incomplete reads from QIODevice



    • Bug
    • Resolution: Done
    • P3: Somewhat important
    • 5.7.0 Alpha
    • 5.4.0
    • Core: I/O
    • None
    • 7d257aab8175f3d4fd3b838d4f47457a8d868885,184d66caa5f7f93b7383319c5c8985524e0dc824


      If QDataStream encounters a ReadPastEnd it should reset the device to the last known state, putting back any characters that have not successfully been parsed yet.

      Use Case

      It should be straightforward to use a QDataStream e.g. for communication over a socket. However, all relevant network protocols are packet based, and it's not guaranteed that the chunks of data arriving with readyRead can be de-serialized completely.

      If one tries to process an incomplete chunk QDataStream will flag this with ReadPastEnd. However, it won't put any characters back to the device, meaning that the user cannot reasonably recover, and wait for more data to arrive.


      The fortuneclient example works around this by wrapping the streamed data again in a QByteArray, and prepending it with it's own size. However, this relies on the implementation detail that streaming a qint16 will write exactly 2 bytes on the device.

      Proposed fix

      All QDataStream::operator<<() should use e.g. ungetChar to restore the QIODevice data into a consistent state. This way, the user can easily just 'bail out' in case of an uncomplete read, and wait for more data to arrive to continue reading.


        Issue Links

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



              alex1973tr Alex Trotsenko
              kkohne Kai Köhne
              1 Vote for this issue
              6 Start watching this issue



                Gerrit Reviews

                  There are no open Gerrit changes