diff --git a/src/network/ssl/qsslsocket.cpp b/src/network/ssl/qsslsocket.cpp index 05f50af6f1..e3dd6ae0c5 100644 --- a/src/network/ssl/qsslsocket.cpp +++ b/src/network/ssl/qsslsocket.cpp @@ -1630,6 +1630,10 @@ void QSslSocket::startClientEncryption() qWarning("QSslSocket::startClientEncryption: cannot start handshake on non-plain connection"); return; } + if (state() != ConnectedState) { + qWarning("QSslSocket::startClientEncryption: cannot start handshake when not connected"); + return; + } #ifdef QSSLSOCKET_DEBUG qDebug() << "QSslSocket::startClientEncryption()"; #endif diff --git a/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp b/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp index 378aff924d..a83611e2b5 100644 --- a/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp +++ b/tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp @@ -195,6 +195,7 @@ private slots: void setEmptyDefaultConfiguration(); void versionAccessors(); void sslOptions(); + void encryptWithoutConnecting(); static void exitLoop() { @@ -2122,6 +2123,18 @@ void tst_QSslSocket::sslOptions() #endif } +void tst_QSslSocket::encryptWithoutConnecting() +{ + if (!QSslSocket::supportsSsl()) + return; + + QTest::ignoreMessage(QtWarningMsg, + "QSslSocket::startClientEncryption: cannot start handshake when not connected"); + + QSslSocket sock; + sock.startClientEncryption(); +} + #endif // QT_NO_OPENSSL QTEST_MAIN(tst_QSslSocket)