Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a mac the socket api crash #8664

Merged
merged 1 commit into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/8664
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: We fixed a potential crash in the socket api

We fixed a crash in the Mac implementation of the socket api.

https://github.com/owncloud/client/pull/8664
38 changes: 13 additions & 25 deletions src/gui/socketapi/socketapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,22 @@ void SocketApi::slotNewConnection()
{
// Note that on macOS this is not actually a line-based QIODevice, it's a SocketApiSocket which is our
// custom message based macOS IPC.
QIODevice *socket = _localServer.nextPendingConnection();
SocketApiSocket *socket = _localServer.nextPendingConnection();

if (!socket) {
return;
}
qCInfo(lcSocketApi) << "New connection" << socket;
connect(socket, &QIODevice::readyRead, this, &SocketApi::slotReadSocket);
connect(socket, SIGNAL(disconnected()), this, SLOT(onLostConnection()));
connect(socket, &QObject::destroyed, this, &SocketApi::slotSocketDestroyed);
connect(socket, &SocketApiSocket::readyRead, this, &SocketApi::slotReadSocket);
connect(socket, &SocketApiSocket::disconnected, this, [socket] {
qCInfo(lcSocketApi) << "Lost connection " << socket;
// will trigger destroyed in the next execution of the main loop
// a direct removal can cause issues when iterating on _listeners
socket->deleteLater();
});
connect(socket, &SocketApiSocket::destroyed, this, [socket, this] {
_listeners.remove(socket);
});
OC_ASSERT(socket->readAll().isEmpty());

auto listener = QSharedPointer<SocketListener>::create(socket);
Expand All @@ -317,25 +324,9 @@ void SocketApi::slotNewConnection()
}
}

void SocketApi::onLostConnection()
{
qCInfo(lcSocketApi) << "Lost connection " << sender();
sender()->deleteLater();

auto socket = qobject_cast<QIODevice *>(sender());
OC_ASSERT(socket);
_listeners.remove(socket);
}

void SocketApi::slotSocketDestroyed(QObject *obj)
{
QIODevice *socket = static_cast<QIODevice *>(obj);
_listeners.remove(socket);
}

void SocketApi::slotReadSocket()
{
QIODevice *socket = qobject_cast<QIODevice *>(sender());
SocketApiSocket *socket = qobject_cast<SocketApiSocket *>(sender());
OC_ENFORCE(socket);

// Find the SocketListener
Expand Down Expand Up @@ -432,10 +423,7 @@ void SocketApi::slotRegisterPath(const QString &alias)

Folder *f = FolderMan::instance()->folder(alias);
if (f) {
const QString message = buildRegisterPathMessage(removeTrailingSlash(f->path()));
for (const auto &listener : qAsConst(_listeners)) {
listener->sendMessage(message);
}
broadcastMessage(buildRegisterPathMessage(removeTrailingSlash(f->path())));
}

_registeredAliases.insert(alias);
Expand Down
7 changes: 3 additions & 4 deletions src/gui/socketapi/socketapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
#include "socketapisocket_mac.h"
#else
#include <QLocalServer>
typedef QLocalServer SocketApiServer;
using SocketApiServer = QLocalServer;
using SocketApiSocket = QLocalSocket;
#endif

class QUrl;
Expand Down Expand Up @@ -66,8 +67,6 @@ public slots:

private slots:
void slotNewConnection();
void onLostConnection();
void slotSocketDestroyed(QObject *obj);
void slotReadSocket();

static void copyUrlToClipboard(const QString &link);
Expand Down Expand Up @@ -157,7 +156,7 @@ private slots:
QString buildRegisterPathMessage(const QString &path);

QSet<QString> _registeredAliases;
QMap<QIODevice *, QSharedPointer<SocketListener>> _listeners;
QMap<SocketApiSocket *, QSharedPointer<SocketListener>> _listeners;
SocketApiServer _localServer;
};
}
Expand Down