diff options
author | Bradley T. Hughes <bradley.hughes@nokia.com> | 2010-07-08 06:42:27 (GMT) |
---|---|---|
committer | Bradley T. Hughes <bradley.hughes@nokia.com> | 2010-07-08 08:30:18 (GMT) |
commit | 2ef8b92ececbf9d33d7c0b44f46c7c975fb0fdaa (patch) | |
tree | a5d52addbee11e8accf79ea4a9cc44331388f841 /src/corelib/plugin | |
parent | ad957b76682dfddb2576154f56b5017680819eef (diff) | |
download | Qt-2ef8b92ececbf9d33d7c0b44f46c7c975fb0fdaa.zip Qt-2ef8b92ececbf9d33d7c0b44f46c7c975fb0fdaa.tar.gz Qt-2ef8b92ececbf9d33d7c0b44f46c7c975fb0fdaa.tar.bz2 |
QUUid::createUuid() should not generate identical sequences on UNIX
An unintended side-effect of commit
90a082c9076f35dcca092ade019891e92692710e is that if qrand() is used
without being seeded first, then createUuid() would always generate the
same sequence. This quite likely to happen considering the Qt does not
actually seed the PRNG, but does use it in many places (we do not call
qsrand(), but we do often call qrand()).
Fix this by changing qrand() to calculate a seed, instead of defaulting to
1. This allows us to remove the qsrand() overload with no arguments, since
qrand() will now seed automatically unless manually seeded by the programmer.
Task-number: QTBUG-11213
Reviewed-by: thiago
Diffstat (limited to 'src/corelib/plugin')
-rw-r--r-- | src/corelib/plugin/quuid.cpp | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index 8541c7d..6a7d35c 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -577,23 +577,19 @@ QUuid QUuid::createUuid() QT_BEGIN_INCLUDE_NAMESPACE #include "qdatetime.h" -#include "stdlib.h" // For srand/rand QT_END_INCLUDE_NAMESPACE -extern void qsrand(); // in qglobal.cpp - QUuid QUuid::createUuid() { static const int intbits = sizeof(int)*8; static int randbits = 0; if (!randbits) { + int r = 0; int max = RAND_MAX; - do { ++randbits; } while ((max=max>>1)); + do { ++r; } while ((max=max>>1)); + randbits = r; } - // reseed, but only if not already seeded - qsrand(); - QUuid result; uint *data = &(result.data1); int chunks = 16 / sizeof(uint); @@ -601,7 +597,7 @@ QUuid QUuid::createUuid() uint randNumber = 0; for (int filled = 0; filled < intbits; filled += randbits) randNumber |= qrand()<<filled; - *(data+chunks) = randNumber; + *(data+chunks) = randNumber; } result.data4[0] = (result.data4[0] & 0x3F) | 0x80; // UV_DCE |