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 <simon.hausmann@qt.io>
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Thiago Macieira 2020-04-08 15:36:01 -03:00
parent fa463fa721
commit 36feab8bf4
2 changed files with 7 additions and 2 deletions

View File

@ -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();
}

View File

@ -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)