From c62c335be44a8b71f590f1ee1c13a8d5c771e3c6 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Thu, 2 Nov 2023 14:36:02 +0100 Subject: [PATCH] QSharedMemory: fix semaphore creation in legacy mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The legacy mode was using the new nativeKey to create a semaphore. As a result, on macOS the name of the semaphore key file was the same as the name of the shared memory key file. This lead to a situation when the file was mistakenly deleted when destroying any instance of the shared memory, not only the one that created it. Fix it by using QSystemSemaphore::legacyNativeKey() in legacy mode. Add a test to verify that we cannot re-create a shared memory with the same key. The test was failing on macOS without the fix. Fixes: QTBUG-111855 Change-Id: Ib0bc41791e889b1888bbb8aa9044c6b053b63a5a Reviewed-by: Tor Arne Vestbø Reviewed-by: Thiago Macieira (cherry picked from commit 02c42b26e1ff94047657c4838128cb5b22d24d2b) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/ipc/qsharedmemory.cpp | 6 ++++- .../ipc/qsharedmemory/tst_qsharedmemory.cpp | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/corelib/ipc/qsharedmemory.cpp b/src/corelib/ipc/qsharedmemory.cpp index 8dcdc43e180..379379464e1 100644 --- a/src/corelib/ipc/qsharedmemory.cpp +++ b/src/corelib/ipc/qsharedmemory.cpp @@ -258,7 +258,11 @@ bool QSharedMemoryPrivate::initKey(SemaphoreAccessMode mode) if (!cleanHandle()) return false; #if QT_CONFIG(systemsemaphore) - systemSemaphore.setNativeKey(semaphoreNativeKey(), 1, mode); + const QString legacyKey = QNativeIpcKeyPrivate::legacyKey(nativeKey); + const QNativeIpcKey semKey = legacyKey.isEmpty() + ? semaphoreNativeKey() + : QSystemSemaphore::legacyNativeKey(legacyKey, nativeKey.type()); + systemSemaphore.setNativeKey(semKey, 1, mode); if (systemSemaphore.error() != QSystemSemaphore::NoError) { QString function = "QSharedMemoryPrivate::initKey"_L1; errorString = QSharedMemory::tr("%1: unable to set key on lock (%2)") diff --git a/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp b/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp index 4c871e02699..cdbc4e5d8b3 100644 --- a/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp +++ b/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp @@ -78,6 +78,9 @@ private slots: void uniqueKey_data(); void uniqueKey(); + // legacy + void createWithSameKey(); + protected: void remove(const QNativeIpcKey &key); @@ -899,6 +902,29 @@ void tst_QSharedMemory::uniqueKey() QCOMPARE(nativeEqual, setEqual); } +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED +void tst_QSharedMemory::createWithSameKey() +{ + const QString key = u"legacy_key"_s; + const qsizetype sz = 100; + QSharedMemory mem1(key); + QVERIFY(mem1.create(sz)); + + { + QSharedMemory mem2(key); + QVERIFY(!mem2.create(sz)); + QVERIFY(mem2.attach()); + } + // and the second create() should fail as well, QTBUG-111855 + { + QSharedMemory mem2(key); + QVERIFY(!mem2.create(sz)); + QVERIFY(mem2.attach()); + } +} +QT_WARNING_POP + QTEST_MAIN(tst_QSharedMemory) #include "tst_qsharedmemory.moc"