From f04657e7e7447d20554772fa80f8f44d73f6d402 Mon Sep 17 00:00:00 2001 From: Jim Broadus Date: Thu, 8 Apr 2021 22:58:26 -0700 Subject: [PATCH] Replace qrand usage with QRandomGenerator QRandomGenerator was introduced in 5.10 and qrand has since been deprecated. QRandomGenerator::global() returns a global instance that has been securely seeded. QRandomGenerator provides methods that generate values within ranges, so taking a modulus of the result isn't necessary. --- ext/clementine-tagreader/main.cpp | 3 +++ ext/libclementine-common/core/closure.cpp | 9 +++++++++ ext/libclementine-common/core/workerpool.h | 9 +++++++++ src/core/crashreporting.cpp | 10 ++++++++++ src/core/utilities.cpp | 8 ++++++++ src/globalsearch/librarysearchprovider.cpp | 12 ++++++++++++ src/globalsearch/simplesearchprovider.cpp | 9 +++++++++ src/main.cpp | 5 +++++ src/playlist/playlist.cpp | 11 +++++++++-- src/ui/networkremotesettingspage.cpp | 10 ++++++++++ src/visualisations/projectmvisualisation.cpp | 12 +++++++++++- 11 files changed, 95 insertions(+), 3 deletions(-) diff --git a/ext/clementine-tagreader/main.cpp b/ext/clementine-tagreader/main.cpp index fcf39f0c9..a107b9c0d 100644 --- a/ext/clementine-tagreader/main.cpp +++ b/ext/clementine-tagreader/main.cpp @@ -39,10 +39,13 @@ int main(int argc, char** argv) { return 1; } +// Use QRandomGenerator starting in 5.10. +#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) // Seed random number generator timeval time; gettimeofday(&time, nullptr); qsrand((time.tv_sec * 1000) + (time.tv_usec / 1000)); +#endif logging::Init(); qLog(Info) << "TagReader worker connecting to" << args[1]; diff --git a/ext/libclementine-common/core/closure.cpp b/ext/libclementine-common/core/closure.cpp index aa5ea98e0..e3fb8fbc1 100644 --- a/ext/libclementine-common/core/closure.cpp +++ b/ext/libclementine-common/core/closure.cpp @@ -19,6 +19,10 @@ #include "core/timeconstants.h" +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) +#include +#endif + namespace _detail { ClosureBase::ClosureBase(ObjectHelper* helper) : helper_(helper) {} @@ -67,6 +71,11 @@ void DoAfter(QObject* receiver, const char* slot, int msec) { } void DoInAMinuteOrSo(QObject* receiver, const char* slot) { + // qrand is deprecated in 5.15 +#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) int msec = (60 + (qrand() % 60)) * kMsecPerSec; +#else + int msec = QRandomGenerator::global()->bounded(60, 120) * kMsecPerSec; +#endif DoAfter(receiver, slot, msec); } diff --git a/ext/libclementine-common/core/workerpool.h b/ext/libclementine-common/core/workerpool.h index c4f905bb3..b9c0ca32e 100644 --- a/ext/libclementine-common/core/workerpool.h +++ b/ext/libclementine-common/core/workerpool.h @@ -29,6 +29,10 @@ #include #include +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) +#include +#endif + #include "clementine-config.h" #include "core/closure.h" #include "core/logging.h" @@ -270,7 +274,12 @@ void WorkerPool::StartOneWorker(Worker* worker) { // Create a server, find an unused name and start listening forever { +#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) const int unique_number = qrand() ^ ((int)(quint64(this) & 0xFFFFFFFF)); +#else + // The global generator is securely seeded. + const int unique_number = QRandomGenerator::global()->generate(); +#endif const QString name = QString("%1_%2").arg(local_server_name_).arg(unique_number); diff --git a/src/core/crashreporting.cpp b/src/core/crashreporting.cpp index b9218a85c..f9d83a54e 100644 --- a/src/core/crashreporting.cpp +++ b/src/core/crashreporting.cpp @@ -30,6 +30,10 @@ #include #include +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) +#include +#endif + #include "config.h" #include "core/logging.h" @@ -158,7 +162,13 @@ void CrashSender::RedirectFinished() { // Find a boundary that doesn't exist in the file QByteArray boundary; forever { +#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) boundary = "--------------" + QString::number(qrand(), 16).toAscii(); +#else + boundary = + "--------------" + + QString::number(QRandomGenerator::global()->generate(), 16).toAscii(); +#endif if (!file_data.contains(boundary)) { break; } diff --git a/src/core/utilities.cpp b/src/core/utilities.cpp index 83efbc08e..5541b3d57 100644 --- a/src/core/utilities.cpp +++ b/src/core/utilities.cpp @@ -52,6 +52,10 @@ #include "core/logging.h" #include "core/timeconstants.h" +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) +#include +#endif + #if defined(Q_OS_UNIX) #include #elif defined(Q_OS_WIN32) @@ -533,7 +537,11 @@ bool IsMouseEventInWidget(const QMouseEvent* e, const QWidget* widget) { quint16 PickUnusedPort() { forever { +#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) const quint16 port = 49152 + qrand() % 16384; +#else + const quint16 port = QRandomGenerator::global()->bounded(49152, 65536); +#endif QTcpServer server; if (server.listen(QHostAddress::Any, port)) { diff --git a/src/globalsearch/librarysearchprovider.cpp b/src/globalsearch/librarysearchprovider.cpp index 7e04ed496..dbfdf708c 100644 --- a/src/globalsearch/librarysearchprovider.cpp +++ b/src/globalsearch/librarysearchprovider.cpp @@ -19,6 +19,10 @@ #include +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) +#include +#endif + #include "core/logging.h" #include "covers/albumcoverloader.h" #include "library/librarybackend.h" @@ -100,7 +104,11 @@ QStringList LibrarySearchProvider::GetSuggestions(int count) { LibraryQuery q; q.SetColumnSpec("artist, album"); q.SetIncludeUnavailable(true); +#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) q.AddWhere("ROWID", qrand() % largest_rowid); +#else + q.AddWhere("ROWID", QRandomGenerator::global()->bounded(largest_rowid)); +#endif q.SetLimit(1); if (!backend_->ExecQuery(&q) || !q.Next()) { @@ -111,7 +119,11 @@ QStringList LibrarySearchProvider::GetSuggestions(int count) { const QString album = q.Value(1).toString(); if (!artist.isEmpty() && !album.isEmpty()) +#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) ret << ((qrand() % 2 == 0) ? artist : album); +#else + ret << (QRandomGenerator::global()->bounded(2) == 0 ? artist : album); +#endif else if (!artist.isEmpty()) ret << artist; else if (!album.isEmpty()) diff --git a/src/globalsearch/simplesearchprovider.cpp b/src/globalsearch/simplesearchprovider.cpp index d8ffeab3d..88f6d2044 100644 --- a/src/globalsearch/simplesearchprovider.cpp +++ b/src/globalsearch/simplesearchprovider.cpp @@ -20,6 +20,10 @@ #include "core/logging.h" #include "playlist/songmimedata.h" +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) +#include +#endif + const int SimpleSearchProvider::kDefaultResultLimit = 6; SimpleSearchProvider::Item::Item(const QString& title, const QUrl& url, @@ -109,7 +113,12 @@ QStringList SimpleSearchProvider::GetSuggestions(int count) { break; } +#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) const Item& item = items_[qrand() % items_.count()]; +#else + const Item& item = + items_[QRandomGenerator::global()->bounded(items_.count())]; +#endif if (!item.keyword_.isEmpty()) ret << item.keyword_; if (!item.metadata_.title().isEmpty()) ret << item.metadata_.title(); } diff --git a/src/main.cpp b/src/main.cpp index 92232e3bc..f5c34584d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -321,8 +321,13 @@ int main(int argc, char* argv[]) { // Seed the random number generators. time_t t = time(nullptr); + // rand is still used in 3rdParty srand(t); +#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) + // Deprecated in 1.15. Starting in 5.10, QRandomGenerator::global provides a + // securely seeded PRNG. qsrand(t); +#endif IncreaseFDLimit(); diff --git a/src/playlist/playlist.cpp b/src/playlist/playlist.cpp index c4c20ec2a..fc41481f9 100644 --- a/src/playlist/playlist.cpp +++ b/src/playlist/playlist.cpp @@ -34,6 +34,10 @@ #include #include +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) +#include +#endif + #include "core/application.h" #include "core/closure.h" #include "core/logging.h" @@ -2032,8 +2036,11 @@ void Playlist::Shuffle() { const int count = items_.count(); for (int i = begin; i < count; ++i) { - int new_pos = i + (rand() % (count - i)); - +#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) + int new_pos = i + (qrand() % (count - i)); +#else + int new_pos = QRandomGenerator::global()->bounded(i, count); +#endif std::swap(new_items[i], new_items[new_pos]); } diff --git a/src/ui/networkremotesettingspage.cpp b/src/ui/networkremotesettingspage.cpp index bfbbe18ec..aa94546ff 100644 --- a/src/ui/networkremotesettingspage.cpp +++ b/src/ui/networkremotesettingspage.cpp @@ -25,6 +25,10 @@ #include #include +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) +#include +#endif + #include "core/application.h" #include "networkremote/networkremote.h" #include "networkremote/networkremotehelper.h" @@ -106,7 +110,13 @@ void NetworkRemoteSettingsPage::Load() { // Auth Code, 5 digits ui_->use_auth_code->setChecked(s.value("use_auth_code", false).toBool()); +#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) ui_->auth_code->setValue(s.value("auth_code", qrand() % 100000).toInt()); +#else + ui_->auth_code->setValue( + s.value("auth_code", QRandomGenerator::global()->bounded(100000)) + .toInt()); +#endif ui_->allow_downloads->setChecked(s.value("allow_downloads", false).toBool()); ui_->convert_lossless->setChecked( diff --git a/src/visualisations/projectmvisualisation.cpp b/src/visualisations/projectmvisualisation.cpp index 993894520..abc94289e 100644 --- a/src/visualisations/projectmvisualisation.cpp +++ b/src/visualisations/projectmvisualisation.cpp @@ -34,6 +34,10 @@ #include "projectmpresetmodel.h" #include "visualisationcontainer.h" +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) +#include +#endif + #ifdef USE_SYSTEM_PROJECTM #include #else @@ -125,7 +129,13 @@ void ProjectMVisualisation::InitProjectM() { // Start at a random preset. if (projectm_->getPlaylistSize() > 0) { - projectm_->selectPreset(qrand() % projectm_->getPlaylistSize(), true); +#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) + int selection = qrand() % projectm_->getPlaylistSize(); +#else + int selection = + QRandomGenerator::global()->bounded(projectm_->getPlaylistSize()); +#endif + projectm_->selectPreset(selection, true); } if (font_path.isNull()) {