diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2017-04-13 21:13:52 -0700 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2017-11-08 09:14:03 +0000 |
commit | 19b0ce5daa31e2ffebfcf2701143742302f1deb4 (patch) | |
tree | 730cccd80947c60ee4872e16f71d4d48d906c354 /src | |
parent | 59c5f7bd9da63621887260fc45e6669282d71ecd (diff) |
Change almost all other uses of qrand() to QRandomGenerator
The vast majority is actually switched to QRandomGenerator::bounded(),
which gives a mostly uniform distribution over the [0, bound)
range. There are very few floating point cases left, as many of those
that did use floating point did not need to, after all. (I did leave
some that were too ugly for me to understand)
This commit also found a couple of calls to rand() instead of qrand().
This commit does not include changes to SSL code that continues to use
qrand() (job for someone else):
src/network/ssl/qsslkey_qt.cpp
src/network/ssl/qsslsocket_mac.cpp
tests/auto/network/ssl/qsslsocket/tst_qsslsocket.cpp
Change-Id: Icd0e0d4b27cb4e5eb892fffd14b5285d43f4afbf
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/io/qprocess_win.cpp | 7 | ||||
-rw-r--r-- | src/corelib/io/qtemporaryfile.cpp | 2 | ||||
-rw-r--r-- | src/gui/image/qpixmap_blitter.cpp | 3 | ||||
-rw-r--r-- | src/gui/opengl/qopenglgradientcache.cpp | 3 | ||||
-rw-r--r-- | src/gui/painting/qpaintengine_raster.cpp | 3 | ||||
-rw-r--r-- | src/network/kernel/qdnslookup.cpp | 11 | ||||
-rw-r--r-- | src/opengl/gl2paintengineex/qglgradientcache.cpp | 3 |
7 files changed, 15 insertions, 17 deletions
diff --git a/src/corelib/io/qprocess_win.cpp b/src/corelib/io/qprocess_win.cpp index 2bbc4eddd0..8da6d6b16e 100644 --- a/src/corelib/io/qprocess_win.cpp +++ b/src/corelib/io/qprocess_win.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. +** Copyright (C) 2017 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -48,6 +48,7 @@ #include <qdir.h> #include <qelapsedtimer.h> #include <qfileinfo.h> +#include <qrandom.h> #include <qregexp.h> #include <qwineventnotifier.h> #include <private/qsystemlibrary_p.h> @@ -99,10 +100,8 @@ static void qt_create_pipe(Q_PIPE *pipe, bool isInputPipe) wchar_t pipeName[256]; unsigned int attempts = 1000; forever { - // ### The user must make sure to call qsrand() to make the pipe names less predictable. - // ### Replace the call to qrand() with a secure version, once we have it in Qt. _snwprintf(pipeName, sizeof(pipeName) / sizeof(pipeName[0]), - L"\\\\.\\pipe\\qt-%X", qrand()); + L"\\\\.\\pipe\\qt-%X", QRandomGenerator::global()->generate()); DWORD dwOpenMode = FILE_FLAG_OVERLAPPED; DWORD dwOutputBufferSize = 0; diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp index 5865d9e19a..b8d3e859cf 100644 --- a/src/corelib/io/qtemporaryfile.cpp +++ b/src/corelib/io/qtemporaryfile.cpp @@ -165,7 +165,7 @@ QFileSystemEntry::NativePath QTemporaryFileName::generateNext() Char *rIter = placeholderEnd; while (rIter != placeholderStart) { - quint32 rnd = QRandomGenerator::generate(); + quint32 rnd = QRandomGenerator::global()->generate(); auto applyOne = [&]() { quint32 v = rnd & ((1 << BitsPerCharacter) - 1); rnd >>= BitsPerCharacter; diff --git a/src/gui/image/qpixmap_blitter.cpp b/src/gui/image/qpixmap_blitter.cpp index de32327071..d694352fc1 100644 --- a/src/gui/image/qpixmap_blitter.cpp +++ b/src/gui/image/qpixmap_blitter.cpp @@ -41,6 +41,7 @@ #include <qpainter.h> #include <qimage.h> +#include <qrandom.h> #include <qscreen.h> #include <private/qguiapplication_p.h> @@ -252,7 +253,7 @@ QImage *QBlittablePlatformPixmap::overlay() m_rasterOverlay->size() != QSize(w,h)){ m_rasterOverlay = new QImage(w,h,QImage::Format_ARGB32_Premultiplied); m_rasterOverlay->fill(0x00000000); - uint color = (qrand() % 11)+7; + uint color = QRandomGenerator::global()->bounded(11)+7; m_overlayColor = QColor(Qt::GlobalColor(color)); m_overlayColor.setAlpha(0x88); diff --git a/src/gui/opengl/qopenglgradientcache.cpp b/src/gui/opengl/qopenglgradientcache.cpp index 58dcbed50a..3aa4c0d2e6 100644 --- a/src/gui/opengl/qopenglgradientcache.cpp +++ b/src/gui/opengl/qopenglgradientcache.cpp @@ -42,6 +42,7 @@ #include <private/qopenglcontext_p.h> #include <private/qrgba64_p.h> #include <QtCore/qmutex.h> +#include <QtCore/qrandom.h> #include "qopenglfunctions.h" #include "qopenglextensions_p.h" @@ -137,7 +138,7 @@ GLuint QOpenGL2GradientCache::addCacheElement(quint64 hash_val, const QGradient { QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); if (cache.size() == maxCacheSize()) { - int elem_to_remove = qrand() % maxCacheSize(); + int elem_to_remove = QRandomGenerator::global()->bounded(maxCacheSize()); quint64 key = cache.keys()[elem_to_remove]; // need to call glDeleteTextures on each removed cache entry: diff --git a/src/gui/painting/qpaintengine_raster.cpp b/src/gui/painting/qpaintengine_raster.cpp index 68554c6579..d0d948bbb7 100644 --- a/src/gui/painting/qpaintengine_raster.cpp +++ b/src/gui/painting/qpaintengine_raster.cpp @@ -50,6 +50,7 @@ #include <qdebug.h> #include <qbitmap.h> #include <qmath.h> +#include <qrandom.h> // #include <private/qdatabuffer_p.h> // #include <private/qpainter_p.h> @@ -4229,7 +4230,7 @@ protected: QSharedPointer<const CacheInfo> addCacheElement(quint64 hash_val, const QGradient &gradient, int opacity) { if (cache.size() == maxCacheSize()) { // may remove more than 1, but OK - cache.erase(cache.begin() + (qrand() % maxCacheSize())); + cache.erase(cache.begin() + QRandomGenerator::global()->bounded(maxCacheSize())); } auto cache_entry = QSharedPointer<CacheInfo>::create(gradient.stops(), opacity, gradient.interpolationMode()); generateGradientColorTable(gradient, cache_entry->buffer64, paletteSize(), opacity); diff --git a/src/network/kernel/qdnslookup.cpp b/src/network/kernel/qdnslookup.cpp index 6203ba37b3..10ff35b72c 100644 --- a/src/network/kernel/qdnslookup.cpp +++ b/src/network/kernel/qdnslookup.cpp @@ -42,7 +42,7 @@ #include <qcoreapplication.h> #include <qdatetime.h> -#include <qthreadstorage.h> +#include <qrandom.h> #include <qurl.h> #include <algorithm> @@ -50,7 +50,6 @@ QT_BEGIN_NAMESPACE Q_GLOBAL_STATIC(QDnsLookupThreadPool, theDnsLookupThreadPool); -Q_GLOBAL_STATIC(QThreadStorage<bool *>, theDnsLookupSeedStorage); static bool qt_qdnsmailexchangerecord_less_than(const QDnsMailExchangeRecord &r1, const QDnsMailExchangeRecord &r2) { @@ -85,7 +84,7 @@ static void qt_qdnsmailexchangerecord_sort(QList<QDnsMailExchangeRecord> &record // Randomize the slice of records. while (!slice.isEmpty()) { - const unsigned int pos = qrand() % slice.size(); + const unsigned int pos = QRandomGenerator::global()->bounded(slice.size()); records[i++] = slice.takeAt(pos); } } @@ -134,7 +133,7 @@ static void qt_qdnsservicerecord_sort(QList<QDnsServiceRecord> &records) // Order the slice of records. while (!slice.isEmpty()) { - const unsigned int weightThreshold = qrand() % (sliceWeight + 1); + const unsigned int weightThreshold = QRandomGenerator::global()->bounded(sliceWeight + 1); unsigned int summedWeight = 0; for (int j = 0; j < slice.size(); ++j) { summedWeight += slice.at(j).weight(); @@ -1011,10 +1010,6 @@ void QDnsLookupRunnable::run() query(requestType, requestName, nameserver, &reply); // Sort results. - if (!theDnsLookupSeedStorage()->hasLocalData()) { - qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()) ^ reinterpret_cast<quintptr>(this)); - theDnsLookupSeedStorage()->setLocalData(new bool(true)); - } qt_qdnsmailexchangerecord_sort(reply.mailExchangeRecords); qt_qdnsservicerecord_sort(reply.serviceRecords); diff --git a/src/opengl/gl2paintengineex/qglgradientcache.cpp b/src/opengl/gl2paintengineex/qglgradientcache.cpp index b4ca49514f..fc5e236ca6 100644 --- a/src/opengl/gl2paintengineex/qglgradientcache.cpp +++ b/src/opengl/gl2paintengineex/qglgradientcache.cpp @@ -41,6 +41,7 @@ #include <private/qdrawhelper_p.h> #include <private/qgl_p.h> #include <QtCore/qmutex.h> +#include <QtCore/qrandom.h> QT_BEGIN_NAMESPACE @@ -130,7 +131,7 @@ GLuint QGL2GradientCache::addCacheElement(quint64 hash_val, const QGradient &gra { QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); if (cache.size() == maxCacheSize()) { - int elem_to_remove = qrand() % maxCacheSize(); + int elem_to_remove = QRandomGenerator::global()->bounded(maxCacheSize()); quint64 key = cache.keys()[elem_to_remove]; // need to call glDeleteTextures on each removed cache entry: |