1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Merge pull request #6166

Maxim Biro (1):
      fix: Usage of random
This commit is contained in:
sudden6 2020-06-08 08:42:22 +02:00
commit 1c29152f9c
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
2 changed files with 23 additions and 17 deletions

View File

@ -34,8 +34,10 @@
#include "util/strongtype.h"
#include <QCoreApplication>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
#include <QRandomGenerator>
#else
#include <QDateTime>
#endif
#include <QRegularExpression>
#include <QString>
@ -667,6 +669,10 @@ void Core::onStarted()
{
ASSERT_CORE_THREAD;
#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))
qsrand(static_cast<uint>(QDateTime::currentMSecsSinceEpoch()));
#endif
// One time initialization stuff
QString name = getUsername();
if (!name.isEmpty()) {
@ -795,10 +801,10 @@ void Core::bootstrapDht()
}
int i = 0;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
static int j = QRandomGenerator::global()->generate() % listSize;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
static int j = QRandomGenerator::global()->bounded(listSize);
#else
static int j = qrand() % listSize;
static int j = static_cast<int>((static_cast<double>(qrand()) / static_cast<double>(RAND_MAX+1l)) * listSize);
#endif
// i think the more we bootstrap, the more we jitter because the more we overwrite nodes
while (i < 2) {
@ -827,7 +833,8 @@ void Core::bootstrapDht()
PARSE_ERR(error);
}
++j;
// bootstrap off every 5th node (+ a special case to avoid cycles when listSize % 5 == 0)
j += 5 + !(listSize % 5);
++i;
}
}

View File

@ -23,8 +23,10 @@
#include <QDebug>
#include <QFile>
#include <QMessageBox>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
#include <QRandomGenerator>
#else
#include <QDateTime>
#endif
#include "src/core/core.h"
@ -102,19 +104,16 @@ void PrivacyForm::showEvent(QShowEvent*)
void PrivacyForm::on_randomNosapamButton_clicked()
{
QTime time = QTime::currentTime();
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
QRandomGenerator(static_cast<uint>(time.msec()));
#else
qsrand(static_cast<uint>(time.msec()));
#endif
uint32_t newNospam{0};
for (int i = 0; i < 4; ++i)
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
newNospam = (newNospam << 8) + (QRandomGenerator::global()->generate() % 256); // Generate byte by byte. For some reason.
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
// guarantees to give a random 32-bit unsigned integer
newNospam = QRandomGenerator::global()->generate();
#else
newNospam = (newNospam << 8) + (qrand() % 256); // Generate byte by byte. For some reason.
qsrand(static_cast<uint>(QDateTime::currentMSecsSinceEpoch()));
for (int i = 0; i < 4; ++i)
// Generate byte by byte, as qrand() is guaranteed to have only 15 bits of randomness (RAND_MAX is guaranteed to be 2^15)
newNospam = (newNospam << 8) + (static_cast<int>((static_cast<double>(qrand()) / static_cast<double>(RAND_MAX+1l)) * 256));
#endif
core->setNospam(newNospam);