diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2017-04-13 23:12:46 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2017-06-12 06:14:43 +0000 |
commit | 5483b30868e44bc0799d7a1998f1907775f50fec (patch) | |
tree | e003cbc415a72501ba7f163aab9ed580a08ab327 /src/corelib/io/qtemporaryfile.cpp | |
parent | 769722778120623e2a873a14e3db2e5e53098fce (diff) |
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 <lars.knoll@qt.io>
Diffstat (limited to 'src/corelib/io/qtemporaryfile.cpp')
-rw-r--r-- | src/corelib/io/qtemporaryfile.cpp | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp index 8a99873fee..efab2a30ff 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 |