Skip to content

Commit f0c0dfd

Browse files
committed
Fix a mac crash the socket api
On mac SocketApiSocket::writeData can emit disconnected() This caused a modification of the map containing the sockets during the iteration and thus a crash.
1 parent 6ce2948 commit f0c0dfd

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

changelog/unreleased/8664

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Bugfix: We fixed a potential crash in the socket api
2+
3+
We fixed a crash in the Mac implementation of the socket api.
4+
5+
https://github.com/owncloud/client/pull/8664

src/gui/socketapi/socketapi.cpp

+13-25
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,22 @@ void SocketApi::slotNewConnection()
296296
{
297297
// Note that on macOS this is not actually a line-based QIODevice, it's a SocketApiSocket which is our
298298
// custom message based macOS IPC.
299-
QIODevice *socket = _localServer.nextPendingConnection();
299+
SocketApiSocket *socket = _localServer.nextPendingConnection();
300300

301301
if (!socket) {
302302
return;
303303
}
304304
qCInfo(lcSocketApi) << "New connection" << socket;
305-
connect(socket, &QIODevice::readyRead, this, &SocketApi::slotReadSocket);
306-
connect(socket, SIGNAL(disconnected()), this, SLOT(onLostConnection()));
307-
connect(socket, &QObject::destroyed, this, &SocketApi::slotSocketDestroyed);
305+
connect(socket, &SocketApiSocket::readyRead, this, &SocketApi::slotReadSocket);
306+
connect(socket, &SocketApiSocket::disconnected, this, [socket] {
307+
qCInfo(lcSocketApi) << "Lost connection " << socket;
308+
// will trigger destroyed in the next execution of the main loop
309+
// a direct removal can cause issues when iterating on _listeners
310+
socket->deleteLater();
311+
});
312+
connect(socket, &SocketApiSocket::destroyed, this, [socket, this] {
313+
_listeners.remove(socket);
314+
});
308315
OC_ASSERT(socket->readAll().isEmpty());
309316

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

320-
void SocketApi::onLostConnection()
321-
{
322-
qCInfo(lcSocketApi) << "Lost connection " << sender();
323-
sender()->deleteLater();
324-
325-
auto socket = qobject_cast<QIODevice *>(sender());
326-
OC_ASSERT(socket);
327-
_listeners.remove(socket);
328-
}
329-
330-
void SocketApi::slotSocketDestroyed(QObject *obj)
331-
{
332-
QIODevice *socket = static_cast<QIODevice *>(obj);
333-
_listeners.remove(socket);
334-
}
335-
336327
void SocketApi::slotReadSocket()
337328
{
338-
QIODevice *socket = qobject_cast<QIODevice *>(sender());
329+
SocketApiSocket *socket = qobject_cast<SocketApiSocket *>(sender());
339330
OC_ENFORCE(socket);
340331

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

433424
Folder *f = FolderMan::instance()->folder(alias);
434425
if (f) {
435-
const QString message = buildRegisterPathMessage(removeTrailingSlash(f->path()));
436-
for (const auto &listener : qAsConst(_listeners)) {
437-
listener->sendMessage(message);
438-
}
426+
broadcastMessage(buildRegisterPathMessage(removeTrailingSlash(f->path())));
439427
}
440428

441429
_registeredAliases.insert(alias);

src/gui/socketapi/socketapi.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
#include "socketapisocket_mac.h"
2727
#else
2828
#include <QLocalServer>
29-
typedef QLocalServer SocketApiServer;
29+
using SocketApiServer = QLocalServer;
30+
using SocketApiSocket = QLocalSocket;
3031
#endif
3132

3233
class QUrl;
@@ -66,8 +67,6 @@ public slots:
6667

6768
private slots:
6869
void slotNewConnection();
69-
void onLostConnection();
70-
void slotSocketDestroyed(QObject *obj);
7170
void slotReadSocket();
7271

7372
static void copyUrlToClipboard(const QString &link);
@@ -157,7 +156,7 @@ private slots:
157156
QString buildRegisterPathMessage(const QString &path);
158157

159158
QSet<QString> _registeredAliases;
160-
QMap<QIODevice *, QSharedPointer<SocketListener>> _listeners;
159+
QMap<SocketApiSocket *, QSharedPointer<SocketListener>> _listeners;
161160
SocketApiServer _localServer;
162161
};
163162
}

0 commit comments

Comments
 (0)