diff options
author | Sérgio Martins <sergio.martins@kdab.com> | 2016-08-28 21:34:32 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2016-10-06 21:20:33 +0000 |
commit | dd00f6dd91a28f3e76bc4c3e894174efffc7f201 (patch) | |
tree | 7c92b7fea6b8a9d1cf12b00f2a22f546012e3bcc /examples/corelib/ipc/localfortuneclient | |
parent | f9acbaccde278eaf44a5324c2a63a99a4cccfb1c (diff) |
Handle short reads in the local sockets example
Protection against short reads was already half implemented,
blockSize was being sent by the server but never used by the client.
Also, blockSize was bumped to quint32: If you're in a position where
short reads can happen then quint16 is probably not enough to hold the
size of your data. On Linux I could only reproduce short reads for
messages > 500K.
Change-Id: I191a3d781da1d8a119debbdafae641c8340a1da2
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Diffstat (limited to 'examples/corelib/ipc/localfortuneclient')
-rw-r--r-- | examples/corelib/ipc/localfortuneclient/client.cpp | 5 | ||||
-rw-r--r-- | examples/corelib/ipc/localfortuneclient/client.h | 2 |
2 files changed, 4 insertions, 3 deletions
diff --git a/examples/corelib/ipc/localfortuneclient/client.cpp b/examples/corelib/ipc/localfortuneclient/client.cpp index 853cb2b087..84c0db9a0e 100644 --- a/examples/corelib/ipc/localfortuneclient/client.cpp +++ b/examples/corelib/ipc/localfortuneclient/client.cpp @@ -100,12 +100,13 @@ void Client::readFortune() in.setVersion(QDataStream::Qt_4_0); if (blockSize == 0) { - if (socket->bytesAvailable() < (int)sizeof(quint16)) + // Relies on the fact that QDataStream format streams a quint32 into sizeof(quint32) bytes + if (socket->bytesAvailable() < (int)sizeof(quint32)) return; in >> blockSize; } - if (in.atEnd()) + if (socket->bytesAvailable() < blockSize || in.atEnd()) return; QString nextFortune; diff --git a/examples/corelib/ipc/localfortuneclient/client.h b/examples/corelib/ipc/localfortuneclient/client.h index f12957fe33..8ba767b4fa 100644 --- a/examples/corelib/ipc/localfortuneclient/client.h +++ b/examples/corelib/ipc/localfortuneclient/client.h @@ -76,7 +76,7 @@ private: QLocalSocket *socket; QString currentFortune; - quint16 blockSize; + quint32 blockSize; }; #endif |