From 5483b30868e44bc0799d7a1998f1907775f50fec Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 13 Apr 2017 23:12:46 -0700 Subject: [PATCH] QTemporaryFile: fix the generation of names from templates First and most importantly, let's not use more than half of the template for the application's PID. With over 71% of all PIDs on a typical Linux system and 90% of those on a Darwin system having 5 decimal digits, using them all in a template that is usually 6 characters long is wasteful. That leaves only 1 character for the random part, thereby reducing the number of temporary files possible to only 52. So limit the PID to half the characters of the template. Second, let's use QRandomGenerator::bounded to create the the random part, instead of qrand (which is often unseeded at this point). Change-Id: Icd0e0d4b27cb4e5eb892fffd14b52eda5e467395 Reviewed-by: Lars Knoll --- src/corelib/io/qtemporaryfile.cpp | 7 +++++-- .../auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp | 4 ---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp index 8a99873fee1..efab2a30ff4 100644 --- a/src/corelib/io/qtemporaryfile.cpp +++ b/src/corelib/io/qtemporaryfile.cpp @@ -42,6 +42,7 @@ #ifndef QT_NO_TEMPORARYFILE #include "qplatformdefs.h" +#include "qrandom.h" #include "private/qtemporaryfile_p.h" #include "private/qfile_p.h" #include "private/qsystemerror_p.h" @@ -131,15 +132,17 @@ static bool createFileFromTemplate(NativeFileHandle &file, Char *rIter = placeholderEnd; #if defined(QT_BUILD_CORE_LIB) + // don't consume more than half of the template with the PID + Char *pidStart = placeholderEnd - (placeholderEnd - placeholderStart) / 2; quint64 pid = quint64(QCoreApplication::applicationPid()); do { *--rIter = Latin1Char((pid % 10) + '0'); pid /= 10; - } while (rIter != placeholderStart && pid != 0); + } while (rIter != pidStart && pid != 0); #endif while (rIter != placeholderStart) { - char ch = char((qrand() & 0xffff) % (26 + 26)); + char ch = char(QRandomGenerator::bounded(26 + 26)); if (ch < 26) *--rIter = Latin1Char(ch + 'A'); else diff --git a/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp b/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp index 59cd3a84118..17bcde49924 100644 --- a/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp +++ b/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp @@ -866,8 +866,6 @@ void tst_QTemporaryFile::guaranteeUnique() // First pass. See which filename QTemporaryFile will try first. { - // Fix the random seed. - qsrand(1135); QTemporaryFile tmpFile("testFile1.XXXXXX"); tmpFile.open(); takenFileName = tmpFile.fileName(); @@ -881,8 +879,6 @@ void tst_QTemporaryFile::guaranteeUnique() // Second pass, now we have blocked its first attempt with a directory. { - // Fix the random seed. - qsrand(1135); QTemporaryFile tmpFile("testFile1.XXXXXX"); QVERIFY(tmpFile.open()); QString uniqueFileName = tmpFile.fileName();