Deleting QSslSocket obj causes crash.



  • Hi was wondering if anybody understands this problem I am seeing.

    I have a simple SSL client and server. Connection is fine. Communication is fine. When the client disconnects the server needs to delete the QSslSocket object, I would imagine. However when I do that the server seg faults. I don't understand what's going on. I expected this to be really straightforward but apparently i missing some QT (or other) concept.

    Can anybody help out? Thanks

    Essential server code:
    @
    void SSLServer::incomingConnection(int sd)
    {
    sslSocket = new SSLSocket(this);
    if( sslSocket->setSocketDescriptor(sd))
    {
    QFile sslkeyfile(privKey_);
    sslSocket->setPrivateKey(QSslKey(sslkeyfile.readAll(),QSsl::Rsa));

        QFile cliCertFile(serverCert_);
        sslSocket->setLocalCertificate(QSslCertificate(cliCertFile.readAll()));
    
        QFile certFile(caCert_);
        sslSocket->addCaCertificate(QSslCertificate(certFile.readAll()));
    
        sslSocket->setPeerVerifyMode(QSslSocket::VerifyPeer);
        sslSocket->setPeerVerifyDepth(1);        
        sslSocket->setProtocol(QSsl::SslV3);
    
        connect(sslSocket, SIGNAL(error(QAbstractSocket::SocketError)),
                       this, SLOT(error_handler(QAbstractSocket::SocketError)));
        connect(sslSocket, SIGNAL(sslErrors(QList<QSslError>)),
                       this, SLOT(ssl_error_handler(QList<QSslError>)));
        connect(sslSocket, SIGNAL(encrypted()), this, 
                      SLOT(ready()));
        connect(sslSocket, SIGNAL(readyRead()), this, 
                      SLOT(read_data_from_client()));
    
        sslSocket->startServerEncryption();
        if(!sslSocket->waitForEncrypted())
        {
            qDebug() <<  "failed to perform SSL handshake with client";
            QList<QSslCertificate> caChain = sslSocket->caCertificates();
            return;
        }
    }
    

    }

    void SSLServer::read_data_from_client()
    {
    QByteArray qstrbytes = sslSocket->readAll();
    qDebug() << Q_FUNC_INFO << qstrbytes;
    }

    void SSLServer::ready()
    {
    QSslCertificate clientCert = sslSocket->peerCertificate();
    qDebug() << clientCert.isValid();
    }

    void SSLServer::error_handler(QAbstractSocket::SocketError in_error)
    {
    qDebug() << Q_FUNC_INFO << in_error;
    if(in_error == QAbstractSocket::RemoteHostClosedError)
    {
    delete sslSocket; //// line causes crash !!!!!!!!!!
    }
    }
    @

    The seg fault happens here in QT's own QAbstractSocketPrivate::resetSocketLayer().
    socketEngine is invalid, garbage pointer, But what am I supposed to do about it.

    @void QAbstractSocketPrivate::resetSocketLayer()
    {
    #if defined (QABSTRACTSOCKET_DEBUG)
    qDebug("QAbstractSocketPrivate::resetSocketLayer()");
    #endif

    if (socketEngine) {
        socketEngine->close(); //// right here crash happens!!!
        socketEngine->disconnect();
        delete socketEngine;
        socketEngine = 0;
        cachedSocketDescriptor = -1;
    }
    if (connectTimer)
        connectTimer->stop();
    if (disconnectTimer)
        disconnectTimer->stop();
    

    }@


  • Lifetime Qt Champion

    Hi,

    Could you test replacing delete with

    @sslSocket->deleteLater();@

    ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.