Replace qrand with QRandomGenerator when using Qt 5.10 or higher

This commit is contained in:
Jonas Kvinge 2020-05-29 17:37:46 +02:00
parent 6c77294a86
commit 5d5723ad58
8 changed files with 63 additions and 16 deletions

View File

@ -40,9 +40,13 @@
#include <QSharedMemory>
#include <QLocalSocket>
#include <QByteArray>
#include <QDateTime>
#include <QElapsedTimer>
#include <QtDebug>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
# include <QRandomGenerator>
#else
# include <QDateTime>
#endif
#include "singleapplication.h"
#include "singleapplication_p.h"
@ -110,8 +114,12 @@ SingleApplication::SingleApplication(int &argc, char *argv[], bool allowSecondar
d->memory->unlock();
// Random sleep here limits the probability of a collision between two racing apps
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
QThread::sleep(QRandomGenerator::global()->bounded(8u, 18u));
#else
qsrand(QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max());
QThread::sleep(8 + static_cast <unsigned long>(static_cast <float>(qrand()) / RAND_MAX * 10));
QThread::sleep(8 + static_cast<unsigned long>(static_cast <float>(qrand()) / RAND_MAX * 10));
#endif
}
if (inst->primary == false) {

View File

@ -40,9 +40,13 @@
#include <QSharedMemory>
#include <QLocalSocket>
#include <QByteArray>
#include <QDateTime>
#include <QElapsedTimer>
#include <QtDebug>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
# include <QRandomGenerator>
#else
# include <QDateTime>
#endif
#include "singlecoreapplication.h"
#include "singlecoreapplication_p.h"
@ -62,8 +66,7 @@ SingleCoreApplication::SingleCoreApplication(int &argc, char *argv[], bool allow
// Store the current mode of the program
d->options = options;
// Generating an application ID used for identifying the shared memory
// block and QLocalServer
// Generating an application ID used for identifying the shared memory block and QLocalServer
d->genBlockServerName();
#ifdef Q_OS_UNIX
@ -111,8 +114,12 @@ SingleCoreApplication::SingleCoreApplication(int &argc, char *argv[], bool allow
d->memory->unlock();
// Random sleep here limits the probability of a collision between two racing apps
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
QThread::sleep(QRandomGenerator::global()->bounded(8u, 18u));
#else
qsrand(QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max());
QThread::sleep(8 + static_cast <unsigned long>(static_cast <float>(qrand()) / RAND_MAX * 10));
QThread::sleep(8 + static_cast<unsigned long>(static_cast <float>(qrand()) / RAND_MAX * 10));
#endif
}
if (inst->primary == false) {

View File

@ -19,6 +19,9 @@
#include <QList>
#include <QTimer>
#include <QGenericArgument>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
# include <QRandomGenerator>
#endif
#include "closure.h"
@ -72,6 +75,13 @@ void DoAfter(QObject *receiver, const char *slot, int msec) {
}
void DoInAMinuteOrSo(QObject *receiver, const char *slot) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
int msec = (60 + QRandomGenerator::global()->bounded(1, 60)) * kMsecPerSec;
#else
int msec = (60 + (qrand() % 60)) * kMsecPerSec;
#endif
DoAfter(receiver, slot, msec);
}

View File

@ -33,6 +33,9 @@
#include <QString>
#include <QStringList>
#include <QAtomicInt>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
# include <QRandomGenerator>
#endif
#include "core/logging.h"
@ -264,7 +267,11 @@ void WorkerPool<HandlerType>::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 = QRandomGenerator::global()->bounded((int)(quint64(this) & 0xFFFFFFFF));
#else
const int unique_number = qrand() ^ ((int)(quint64(this) & 0xFFFFFFFF));
#endif
const QString name = QString("%1_%2").arg(local_server_name_).arg(unique_number);
if (worker->local_server_->listen(name)) {

View File

@ -46,9 +46,11 @@ int main(int argc, char **argv) {
}
// Seed random number generator
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
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];
@ -61,7 +63,9 @@ int main(int argc, char **argv) {
return 1;
}
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
QSslSocket::addDefaultCaCertificates(QSslCertificate::fromPath(":/certs/godaddy-root.pem", QSsl::Pem));
#endif
TagReaderWorker worker(&socket);

View File

@ -35,14 +35,6 @@ void TagReaderWorker::MessageArrived(const pb::tagreader::Message& message) {
pb::tagreader::Message reply;
#if 0
// Crash every few requests
if (qrand() % 10 == 0) {
qLog(Debug) << "Crashing on request ID" << message.id();
abort();
}
#endif
if (message.has_read_file_request()) {
tag_reader_.ReadFile(QStringFromStdString(message.read_file_request().filename()), reply.mutable_read_file_response()->mutable_metadata());
}

View File

@ -64,6 +64,9 @@
#include <QMessageBox>
#include <QNetworkInterface>
#include <QtDebug>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
# include <QRandomGenerator>
#endif
#include <stdio.h>
@ -540,7 +543,11 @@ bool IsMouseEventInWidget(const QMouseEvent *e, const QWidget *widget) {
quint16 PickUnusedPort() {
forever {
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
const quint64 port = QRandomGenerator::global()->bounded(49152, 65535);
#else
const quint16 port = 49152 + qrand() % 16384;
#endif
QTcpServer server;
if (server.listen(QHostAddress::Any, port)) {
@ -821,8 +828,12 @@ QString CryptographicRandomString(const int len) {
QString GetRandomString(const int len, const QString &UseCharacters) {
QString randstr;
for(int i=0 ; i < len ; ++i) {
int index = qrand() % UseCharacters.length();
for(int i = 0 ; i < len ; ++i) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
const int index = QRandomGenerator::global()->bounded(0, UseCharacters.length());
#else
const int index = qrand() % UseCharacters.length();
#endif
QChar nextchar = UseCharacters.at(index);
randstr.append(nextchar);
}

View File

@ -48,6 +48,9 @@
#include <QTcpSocket>
#include <QSslSocket>
#include <QDateTime>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
# include <QRandomGenerator>
#endif
LocalRedirectServer::LocalRedirectServer(QObject *parent)
: QTcpServer(parent),
@ -152,7 +155,12 @@ bool LocalRedirectServer::GenerateCertificate() {
return false;
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
quint64 serial = 9999999 + QRandomGenerator::global()->bounded(1000000);
#else
quint64 serial = (9999999 + qrand() % 1000000);
#endif
QByteArray q_serial;
q_serial.setNum(serial);