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>
This commit is contained in:
parent
f9acbaccde
commit
dd00f6dd91
@ -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;
|
||||
|
@ -76,7 +76,7 @@ private:
|
||||
|
||||
QLocalSocket *socket;
|
||||
QString currentFortune;
|
||||
quint16 blockSize;
|
||||
quint32 blockSize;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -96,10 +96,10 @@ void Server::sendFortune()
|
||||
QByteArray block;
|
||||
QDataStream out(&block, QIODevice::WriteOnly);
|
||||
out.setVersion(QDataStream::Qt_4_0);
|
||||
out << (quint16)0;
|
||||
out << (quint32)0;
|
||||
out << fortunes.at(qrand() % fortunes.size());
|
||||
out.device()->seek(0);
|
||||
out << (quint16)(block.size() - sizeof(quint16));
|
||||
out << (quint32)(block.size() - sizeof(quint32));
|
||||
|
||||
QLocalSocket *clientConnection = server->nextPendingConnection();
|
||||
connect(clientConnection, SIGNAL(disconnected()),
|
||||
|
Loading…
x
Reference in New Issue
Block a user