summaryrefslogtreecommitdiffstats
path: root/src/network
diff options
context:
space:
mode:
authorAlex Trotsenko <alex1973tr@gmail.com>2016-10-24 19:16:13 +0300
committerAlex Trotsenko <alex1973tr@gmail.com>2016-10-27 17:15:41 +0000
commit0e2d38b4477ffec6941d0f88cfa7bf92a8b79843 (patch)
treef4a71dca7c823aa5779e79d07c3e0ead7190d1f4 /src/network
parentd19f01acbf9547762150a479793b84898f1a115e (diff)
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>
Diffstat (limited to 'src/network')
-rw-r--r--src/network/socket/qabstractsocket.cpp4
-rw-r--r--src/network/socket/qnativesocketengine.cpp4
2 files changed, 6 insertions, 2 deletions
diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp
index 3d665270fd..f28dc6698a 100644
--- a/src/network/socket/qabstractsocket.cpp
+++ b/src/network/socket/qabstractsocket.cpp
@@ -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;
diff --git a/src/network/socket/qnativesocketengine.cpp b/src/network/socket/qnativesocketengine.cpp
index 805acde860..86d13d82a2 100644
--- a/src/network/socket/qnativesocketengine.cpp
+++ b/src/network/socket/qnativesocketengine.cpp
@@ -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)
{