From 9f6765897c86dacd75a17bc1808bbf00b1062ad6 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Tue, 7 Nov 2023 11:39:48 +0100 Subject: [PATCH] QSharedMemory: fix attach() -> create() for non-legacy SystemV mode The problem with non-legacy mode is that the backend uses the same filename for the system semaphore file and for the shared memory file. What happens is that when we try to call attach(), a semaphore is created. Later in attach() we set unix_key, because ftok() returns a valid handle (it uses the file which was created for semaphore). After that, an attempt to actually attach to a shared memory fails, but no clean-up is done. So, a later call to create() sees that unix_key is already valid, but it cannot properly clean it, because it does not actually refer to any shared memory. Fix it by cleaning up unix_key and nativeKeyFile if shmget() call in attach() fails. Change-Id: Ibccc3ac307d8b2e07e1b9b24b55f97a859a03131 Reviewed-by: Thiago Macieira (cherry picked from commit e85a3cde2f8aa15eb33f12dc8c52ef660aeb7cc0) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/ipc/qsharedmemory_systemv.cpp | 2 ++ tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp | 3 --- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/corelib/ipc/qsharedmemory_systemv.cpp b/src/corelib/ipc/qsharedmemory_systemv.cpp index ddf9a36cd67..dc9de11091d 100644 --- a/src/corelib/ipc/qsharedmemory_systemv.cpp +++ b/src/corelib/ipc/qsharedmemory_systemv.cpp @@ -160,6 +160,8 @@ bool QSharedMemorySystemV::attach(QSharedMemoryPrivate *self, QSharedMemory::Acc int id = shmget(unix_key, 0, (mode == QSharedMemory::ReadOnly ? 0400 : 0600)); if (-1 == id) { self->setUnixErrorString("QSharedMemory::attach (shmget)"_L1); + unix_key = 0; + nativeKeyFile.clear(); return false; } diff --git a/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp b/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp index 8e7d0064ce7..faa56482b34 100644 --- a/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp +++ b/tests/auto/corelib/ipc/qsharedmemory/tst_qsharedmemory.cpp @@ -575,9 +575,6 @@ void tst_QSharedMemory::attachBeforeCreate() 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)); }