From 3a57243fbffb771c0991c99d93ac9b9fcf4fc4ee Mon Sep 17 00:00:00 2001 From: Shane Kearns Date: Mon, 20 Feb 2012 18:48:10 +0000 Subject: Windows - fix getsockopt calls for narrower than int options Windows unhelpfully writes to only one byte of the output buffer when getsockopt is called for a boolean option. Therefore we have to zero initialise the int rather than initialising to -1 as was done before. This in general only works for little endian architecture, because the word would look like 0x01000000 on big endian. So I have added some compile time asserts in the assumption that windows is always little endian. This is ok for comparisons with 0/false, but not comparisons with true or nonzero values. Task-number: QTBUG-23488 Change-Id: I3c586d1ada76465fc045a82661f289920c657a4c Reviewed-by: Richard J. Moore Reviewed-by: Friedemann Kleint Reviewed-by: Andreas Holzammer (cherry picked from commit 46e4a9d5231e2d9e35424259858713ca539b8e30) --- src/network/socket/qnativesocketengine_win.cpp | 8 +++++-- tests/auto/qtcpsocket/tst_qtcpsocket.cpp | 29 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/network/socket/qnativesocketengine_win.cpp b/src/network/socket/qnativesocketengine_win.cpp index 6ed0ef4..d4b77a6 100644 --- a/src/network/socket/qnativesocketengine_win.cpp +++ b/src/network/socket/qnativesocketengine_win.cpp @@ -429,10 +429,14 @@ int QNativeSocketEnginePrivate::option(QNativeSocketEngine::SocketOption opt) co break; } - int v = -1; +#if Q_BYTE_ORDER != Q_LITTLE_ENDIAN +#error code assumes windows is little endian +#endif + int v = 0; //note: windows doesn't write to all bytes if the option type is smaller than int QT_SOCKOPTLEN_T len = sizeof(v); - if (getsockopt(socketDescriptor, level, n, (char *) &v, &len) != -1) + if (getsockopt(socketDescriptor, level, n, (char *) &v, &len) == 0) return v; + WS_ERROR_DEBUG(WSAGetLastError()); return -1; } diff --git a/tests/auto/qtcpsocket/tst_qtcpsocket.cpp b/tests/auto/qtcpsocket/tst_qtcpsocket.cpp index 4c5dcf6..ee8f43f 100644 --- a/tests/auto/qtcpsocket/tst_qtcpsocket.cpp +++ b/tests/auto/qtcpsocket/tst_qtcpsocket.cpp @@ -208,6 +208,8 @@ private slots: void qtbug14268_peek(); + void setSocketOption(); + protected slots: void nonBlockingIMAP_hostFound(); @@ -2686,7 +2688,34 @@ void tst_QTcpSocket::qtbug14268_peek() QVERIFY(incoming->read(128*1024) == QByteArray("abc\ndef\nghi\n")); } +void tst_QTcpSocket::setSocketOption() +{ + QFETCH_GLOBAL(bool, setProxy); + if (setProxy) + return; + SocketPair socketPair; + QVERIFY(socketPair.create()); + QTcpSocket *outgoing = socketPair.endPoints[0]; + QTcpSocket *incoming = socketPair.endPoints[1]; + + QVERIFY(incoming->state() == QTcpSocket::ConnectedState); + QVERIFY(outgoing->state() == QTcpSocket::ConnectedState); + + outgoing->setSocketOption(QAbstractSocket::LowDelayOption, true); + QVariant v = outgoing->socketOption(QAbstractSocket::LowDelayOption); + QVERIFY(v.isValid() && v.toBool()); + outgoing->setSocketOption(QAbstractSocket::KeepAliveOption, true); + v = outgoing->socketOption(QAbstractSocket::KeepAliveOption); + QVERIFY(v.isValid() && v.toBool()); + + outgoing->setSocketOption(QAbstractSocket::LowDelayOption, false); + v = outgoing->socketOption(QAbstractSocket::LowDelayOption); + QVERIFY(v.isValid() && !v.toBool()); + outgoing->setSocketOption(QAbstractSocket::KeepAliveOption, false); + v = outgoing->socketOption(QAbstractSocket::KeepAliveOption); + QVERIFY(v.isValid() && !v.toBool()); +} QTEST_MAIN(tst_QTcpSocket) #include "tst_qtcpsocket.moc" -- cgit v0.12