QAbstractSocket: avoid unspecified behavior in writing on TCP

Passing zero as size parameter to QAbstractSocketEngine::write() has
unspecified behavior, at least for TCP sockets. This could happen on
flush() when writeBuffer is empty or on writeData() with size 0. Avoid
by explicitly checking against zero size.

Change-Id: I070630d244ce6c3de3da94f84c2cded2c7a4b081
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Alex Trotsenko 2016-10-24 19:16:13 +03:00
parent d19f01acbf
commit 0e2d38b447
2 changed files with 6 additions and 2 deletions

View File

@ -881,7 +881,7 @@ bool QAbstractSocketPrivate::flush()
const char *ptr = writeBuffer.readPointer();
// Attempt to write it all in one chunk.
qint64 written = socketEngine->write(ptr, nextSize);
qint64 written = nextSize ? socketEngine->write(ptr, nextSize) : Q_INT64_C(0);
if (written < 0) {
#if defined (QABSTRACTSOCKET_DEBUG)
qDebug() << "QAbstractSocketPrivate::flush() write error, aborting." << socketEngine->errorString();
@ -2477,7 +2477,7 @@ qint64 QAbstractSocket::writeData(const char *data, qint64 size)
if (!d->isBuffered && d->socketType == TcpSocket
&& d->socketEngine && d->writeBuffer.isEmpty()) {
// This code is for the new Unbuffered QTcpSocket use case
qint64 written = d->socketEngine->write(data, size);
qint64 written = size ? d->socketEngine->write(data, size) : Q_INT64_C(0);
if (written < 0) {
d->setError(d->socketEngine->error(), d->socketEngine->errorString());
return written;

View File

@ -846,6 +846,10 @@ qint64 QNativeSocketEngine::writeDatagram(const char *data, qint64 size, const Q
/*!
Writes a block of \a size bytes from \a data to the socket.
Returns the number of bytes written, or -1 if an error occurred.
Passing zero as the \a size parameter on a connected UDP socket
will send an empty datagram. For other socket types results are
unspecified.
*/
qint64 QNativeSocketEngine::write(const char *data, qint64 size)
{