Check if socketEngine is set before accessing it.

QTcpServer::addPendingConnection() is protected, so it is possible to
add pending connections from a class derived from QTcpServer. This
derived class can get a QTcpSocket from somewhere else, in which case
d->socketEngine will not be set (or used). Given that it is possible to
add pending connections in this scenario, it would make sense to be able
to retrieve them as well.

[ChangeLog][QtNetwork][QTcpServer] Permit using a QTcpServer with
externally created QTcpSocket.

Task-number: QTBUG-51288
Change-Id: I830c10f1a881e2bca4e4ad716d8be865e1c27a9f
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
bb10
Fredrik de Vibe 2016-03-02 16:52:22 +01:00
parent 954bd06bc5
commit 48f25caac4
2 changed files with 23 additions and 1 deletions

View File

@ -543,8 +543,11 @@ QTcpSocket *QTcpServer::nextPendingConnection()
if (d->pendingConnections.isEmpty())
return 0;
if (!d->socketEngine->isReadNotificationEnabled())
if (!d->socketEngine) {
qWarning("QTcpServer::nextPendingConnection() called while not listening");
} else if (!d->socketEngine->isReadNotificationEnabled()) {
d->socketEngine->setReadNotificationEnabled(true);
}
return d->pendingConnections.takeFirst();
}

View File

@ -106,6 +106,8 @@ private slots:
void eagainBlockingAccept();
void qtbug51288();
private:
#ifndef QT_NO_BEARERMANAGEMENT
QNetworkSession *networkSession;
@ -990,5 +992,22 @@ void tst_QTcpServer::eagainBlockingAccept()
server.close();
}
class NonListeningTcpServer : public QTcpServer
{
public:
void addSocketFromOutside(QTcpSocket* s)
{
addPendingConnection(s);
}
};
void tst_QTcpServer::qtbug51288()
{
NonListeningTcpServer server;
QTcpSocket socket;
server.addSocketFromOutside(&socket);
QCOMPARE(&socket, server.nextPendingConnection());
}
QTEST_MAIN(tst_QTcpServer)
#include "tst_qtcpserver.moc"