QWaylandShmBuffer: use make_unique / unique_ptr

This keeps the QFile/QTemporaryFile objects inside a RAII wrapper at
any time, which is safer.

Since QScopedPointer doesn't have a similar function (and because of
upcoming QT_NO_SCOPED_POINTER), also port to std::unique_ptr.

Amends 32daa1a5b9ac06ae89d3aab5b4638139e72814e2.

Pick-to: 6.9 6.8
Change-Id: If67a3a3bdb4fa2a06ecafd1c3dcb90e1121159b0
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2024-12-29 11:18:38 +01:00
parent 138c43a321
commit 6103977654

View File

@ -16,6 +16,8 @@
#include <QtWaylandClient/private/wayland-wayland-client-protocol.h>
#include <memory>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
@ -59,18 +61,19 @@ QWaylandShmBuffer::QWaylandShmBuffer(QWaylandDisplay *display,
fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_SEAL);
#endif
QScopedPointer<QFile> filePointer;
std::unique_ptr<QFile> filePointer;
bool opened;
if (fd == -1) {
auto tmpFile = new QTemporaryFile (QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) +
auto tmpFile =
std::make_unique<QTemporaryFile>(QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) +
QLatin1String("/wayland-shm-XXXXXX"));
opened = tmpFile->open();
filePointer.reset(tmpFile);
filePointer = std::move(tmpFile);
} else {
auto file = new QFile;
auto file = std::make_unique<QFile>();
opened = file->open(fd, QIODevice::ReadWrite | QIODevice::Unbuffered, QFile::AutoCloseHandle);
filePointer.reset(file);
filePointer = std::move(file);
}
// NOTE beginPaint assumes a new buffer be all zeroes, which QFile::resize does.
if (!opened || !filePointer->resize(alloc)) {