QAbstractSocket: do not enable read notifications on TCP in bind()

In bind+connect scenario, rejected connection can trigger a read
notification while the socket is opened. But unlike UDP, reading from
the socket engine or emitting a readyRead() signal is not allowed for
the TCP socket in bound or connecting state.

To make a bind+connect scenario work properly, disable the read
notifications until a connection is established.

Task-number: QTBUG-50124
Change-Id: I7b3d015b0f6021fb9ff9f83560478aa5545f41f5
Reviewed-by: Richard J. Moore <rich@kde.org>
This commit is contained in:
Alex Trotsenko 2015-12-23 13:10:30 +02:00
parent 1e2d35d488
commit 6b6955c2ff
2 changed files with 23 additions and 1 deletions

View File

@ -1530,7 +1530,8 @@ bool QAbstractSocketPrivate::bind(const QHostAddress &address, quint16 port, QAb
localPort = socketEngine->localPort();
emit q->stateChanged(state);
socketEngine->setReadNotificationEnabled(true);
if (socketType == QAbstractSocket::UdpSocket)
socketEngine->setReadNotificationEnabled(true);
return true;
}

View File

@ -200,6 +200,7 @@ private slots:
void setSocketOption();
void clientSendDataOnDelayedDisconnect();
void readNotificationsAfterBind();
protected slots:
void nonBlockingIMAP_hostFound();
@ -2984,5 +2985,25 @@ void tst_QTcpSocket::clientSendDataOnDelayedDisconnect()
delete socket;
}
// Test that the socket does not enable the read notifications in bind()
void tst_QTcpSocket::readNotificationsAfterBind()
{
QFETCH_GLOBAL(bool, setProxy);
if (setProxy)
return;
QAbstractSocket socket(QAbstractSocket::TcpSocket, Q_NULLPTR);
QVERIFY2(socket.bind(), "Bind error!");
connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), &QTestEventLoop::instance(), SLOT(exitLoop()));
QSignalSpy spyReadyRead(&socket, SIGNAL(readyRead()));
socket.connectToHost(QtNetworkSettings::serverName(), 12346);
QTestEventLoop::instance().enterLoop(10);
QVERIFY2(!QTestEventLoop::instance().timeout(), "Connection to closed port timed out instead of refusing, something is wrong");
QVERIFY2(socket.state() == QAbstractSocket::UnconnectedState, "Socket connected unexpectedly!");
QCOMPARE(spyReadyRead.count(), 0);
}
QTEST_MAIN(tst_QTcpSocket)
#include "tst_qtcpsocket.moc"