QSslSocket: reset connection parameters on disconnect

Otherwise socketDescriptor(), localPort(), localAddress(), peerPort(),
peerAddress(), and peerName() remain uncleared until close() is called.

This could take place when the connection is closed by the remote
endpoint or the user calls disconnectFromHost(). After disconnecting,
connection parameters are no longer valid, while I/O device is still
opened and may have pending data for reading. Usually, the user reads
all incoming data and closes the device independently.

Change-Id: Ic898851c39137faf64019949910f0d94ebb79df7
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
bb10
Alex Trotsenko 2016-06-30 18:45:41 +03:00 committed by Timur Pocheptsov
parent 6c935dc74f
commit cddb344f3e
2 changed files with 13 additions and 0 deletions

View File

@ -2401,6 +2401,13 @@ void QSslSocketPrivate::_q_disconnectedSlot()
#endif
disconnected();
emit q->disconnected();
q->setLocalPort(0);
q->setLocalAddress(QHostAddress());
q->setPeerPort(0);
q->setPeerAddress(QHostAddress());
q->setPeerName(QString());
cachedSocketDescriptor = -1;
}
/*!

View File

@ -1213,6 +1213,12 @@ void tst_QTcpSocket::connectDisconnectConnectDisconnect()
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
QCOMPARE(socket->socketType(), QTcpSocket::TcpSocket);
QCOMPARE(socket->socketDescriptor(), qintptr(-1));
QCOMPARE(int(socket->localPort()), 0);
QCOMPARE(socket->localAddress(), QHostAddress());
QCOMPARE(int(socket->peerPort()), 0);
QCOMPARE(socket->peerAddress(), QHostAddress());
socket->connectToHost(QtNetworkSettings::serverName(), 143);
QVERIFY(socket->waitForReadyRead(10000));
QCOMPARE(QString::fromLatin1(socket->read(4)), QString("* OK"));