diff --git a/src/corelib/ipc/qsharedmemory_systemv.cpp b/src/corelib/ipc/qsharedmemory_systemv.cpp index 716b7aedd11..ddf9a36cd67 100644 --- a/src/corelib/ipc/qsharedmemory_systemv.cpp +++ b/src/corelib/ipc/qsharedmemory_systemv.cpp @@ -76,6 +76,7 @@ key_t QSharedMemorySystemV::handle(QSharedMemoryPrivate *self) unix_key = ftok(nativeKeyFile, int(self->nativeKey.type())); if (unix_key < 0) { self->setUnixErrorString("QSharedMemory::handle"_L1); + nativeKeyFile.clear(); unix_key = 0; } return unix_key; diff --git a/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp b/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp index cdbc4e5d8b3..8e7d0064ce7 100644 --- a/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp +++ b/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp @@ -56,6 +56,8 @@ private slots: void removeWhileAttached(); void emptyMemory(); void readOnly(); + void attachBeforeCreate_data(); + void attachBeforeCreate(); // basics all together void simpleProducerConsumer_data(); @@ -546,6 +548,39 @@ void tst_QSharedMemory::readOnly() #endif } +void tst_QSharedMemory::attachBeforeCreate_data() +{ + QTest::addColumn("legacy"); + + QTest::addRow("legacy") << true; + QTest::addRow("non-legacy") << false; +} + +void tst_QSharedMemory::attachBeforeCreate() +{ + QFETCH_GLOBAL(const QNativeIpcKey::Type, keyType); + QFETCH(const bool, legacy); + const QString keyStr(u"test"_s); + QNativeIpcKey key; + if (legacy) { + key = QSharedMemory::legacyNativeKey(keyStr, keyType); + // same as rememberKey(), but with legacy + if (!keys.contains(key)) { + keys.append(key); + remove(key); + } + } else { + key = rememberKey(keyStr); + } + const qsizetype sz = 100; + QSharedMemory mem(key); + QVERIFY(!mem.attach()); + // Fails for all SystemV backends now + if (!legacy && (keyType < QNativeIpcKey::Type::PosixRealtime)) + QEXPECT_FAIL("", "Not fixed yet", Continue); + QVERIFY(mem.create(sz)); +} + /*! Keep making shared memory until the kernel stops us. */