From 36feab8bf49b4c57a45b6668921e8accaa167c36 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 8 Apr 2020 15:36:01 -0300 Subject: [PATCH] QTemporaryFile/Linux: don't cut the root dir's slash Normally people shouldn't create temporary files on /, but if you're running as root, why not? Caught when running tst_qtemporaryfile as root: openat(AT_FDCWD, "", O_RDWR|O_CLOEXEC|O_TMPFILE, 0600) = -1 ENOENT (No such file or directory) Change-Id: Ibdc95e9af7bd456a94ecfffd1603ebfc17cea220 Reviewed-by: Simon Hausmann Reviewed-by: David Faure --- src/corelib/io/qtemporaryfile.cpp | 6 ++++-- tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp index 55d13dad70..c016a622c7 100644 --- a/src/corelib/io/qtemporaryfile.cpp +++ b/src/corelib/io/qtemporaryfile.cpp @@ -284,8 +284,10 @@ createUnnamedFile(NativeFileHandle &file, QTemporaryFileName &tfn, quint32 mode, return CreateUnnamedFileStatus::NotSupported; const char *p = "."; - int lastSlash = tfn.path.lastIndexOf('/'); - if (lastSlash != -1) { + QByteArray::size_type lastSlash = tfn.path.lastIndexOf('/'); + if (lastSlash >= 0) { + if (lastSlash == 0) + lastSlash = 1; tfn.path[lastSlash] = '\0'; p = tfn.path.data(); } diff --git a/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp b/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp index 00417fffa0..cdf79760f6 100644 --- a/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp +++ b/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp @@ -517,6 +517,9 @@ void tst_QTemporaryFile::openOnRootDrives() QTemporaryFile file(driveInfo.filePath() + "XXXXXX.txt"); file.setAutoRemove(true); QVERIFY(file.open()); + + QFileInfo fi(file.fileName()); + QCOMPARE(fi.absoluteDir(), driveInfo.filePath()); } } #if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)