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"