OAuth: Fix crash when closing the browser while identifying
authorOlivier Goffart <ogoffart@woboq.com>
Mon, 25 Sep 2017 14:44:33 +0000 (16:44 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Thu, 5 Oct 2017 20:01:37 +0000 (22:01 +0200)
To reproduce, log in and click "authorize" on the browser, then close
the browser before the client has replied, (but after redirected to localhost,
i.e. when the client is asking the server for the token)

The problem is that socket can be destroyed so we don't need to answer on a
destroyed socket.

src/gui/creds/oauth.cpp

index 05ef093b3e4e3832c3dc185dc536f6307022a5c7..f06fe3a3cd940bc9368e41cde3a16818f968ff16 100644 (file)
@@ -34,6 +34,8 @@ OAuth::~OAuth()
 static void httpReplyAndClose(QTcpSocket *socket, const char *code, const char *html,
     const char *moreHeaders = nullptr)
 {
+    if (!socket)
+        return; // socket can have been deleted if the browser was closed
     socket->write("HTTP/1.1 ");
     socket->write(code);
     socket->write("\r\nContent-Type: text/html\r\nConnection: close\r\nContent-Length: ");
@@ -62,7 +64,7 @@ void OAuth::start()
         return;
 
     QObject::connect(&_server, &QTcpServer::newConnection, this, [this] {
-        while (QTcpSocket *socket = _server.nextPendingConnection()) {
+        while (QPointer<QTcpSocket> socket = _server.nextPendingConnection()) {
             QObject::connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
             QObject::connect(socket, &QIODevice::readyRead, this, [this, socket] {
                 QByteArray peek = socket->peek(qMin(socket->bytesAvailable(), 4000LL)); //The code should always be within the first 4K