QAbstractSocket: do not use internal buffer for discarding the data

Instead, allocate a temporary buffer on the stack. This prevents the
internal read buffer from being allocated if the device is opened only
for writing.

Change-Id: Ib91c58299206e92006589807527e7b71a5555c8f
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
bb10
Alex Trotsenko 2016-01-20 14:03:24 +02:00
parent 1272bd4d4a
commit 3713764099
1 changed files with 29 additions and 16 deletions

View File

@ -468,6 +468,7 @@
#include <qtimer.h>
#include <qelapsedtimer.h>
#include <qscopedvaluerollback.h>
#include <qvarlengtharray.h>
#ifndef QT_NO_SSL
#include <QtNetwork/qsslsocket.h>
@ -1234,27 +1235,39 @@ bool QAbstractSocketPrivate::readFromSocket()
// host has _not_ disappeared).
bytesToRead = 4096;
}
if (readBufferMaxSize && bytesToRead > (readBufferMaxSize - buffer.size()))
bytesToRead = readBufferMaxSize - buffer.size();
if (q->isReadable()) {
if (readBufferMaxSize && bytesToRead > (readBufferMaxSize - buffer.size()))
bytesToRead = readBufferMaxSize - buffer.size();
#if defined(QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocketPrivate::readFromSocket() about to read %lld bytes",
bytesToRead);
qDebug("QAbstractSocketPrivate::readFromSocket() about to read %lld bytes",
bytesToRead);
#endif
// Read from the socket, store data in the read buffer.
char *ptr = buffer.reserve(bytesToRead);
qint64 readBytes = socketEngine->read(ptr, bytesToRead);
if (readBytes == -2) {
// No bytes currently available for reading.
buffer.chop(bytesToRead);
return true;
// Read from the socket, store data in the read buffer.
char *ptr = buffer.reserve(bytesToRead);
qint64 readBytes = socketEngine->read(ptr, bytesToRead);
if (readBytes == -2) {
// No bytes currently available for reading.
buffer.chop(bytesToRead);
return true;
}
buffer.chop(bytesToRead - (readBytes < 0 ? qint64(0) : readBytes));
#if defined(QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocketPrivate::readFromSocket() got %lld bytes, buffer size = %lld",
readBytes, buffer.size());
#endif
} else {
// Discard unwanted data if opened in WriteOnly mode
QVarLengthArray<char, 4096> discardBuffer(bytesToRead);
#if defined(QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocketPrivate::readFromSocket() about to discard %lld bytes",
bytesToRead);
#endif
socketEngine->read(discardBuffer.data(), bytesToRead);
}
buffer.chop(bytesToRead - ((readBytes < 0 || !q->isReadable()) ? qint64(0) : readBytes));
#if defined(QABSTRACTSOCKET_DEBUG)
qDebug("QAbstractSocketPrivate::readFromSocket() got %lld bytes, buffer size = %lld",
readBytes, buffer.size());
#endif
if (!socketEngine->isValid()) {
setErrorAndEmit(socketEngine->error(), socketEngine->errorString());