Use QRandomGenerator instead of qrand

Signed-off-by: Felix Weilbach <felix.weilbach@nextcloud.com>
This commit is contained in:
Felix Weilbach 2021-09-03 21:37:46 +02:00 committed by Felix Weilbach (Rebase PR Action)
parent 2fe3a7947e
commit 0af83dd1b6
11 changed files with 22 additions and 21 deletions

View file

@ -317,8 +317,6 @@ int main(int argc, char **argv)
qputenv("OPENSSL_CONF", opensslConf.toLocal8Bit());
#endif
qsrand(std::random_device()());
CmdOptions options;
options.silent = false;
options.trustSSL = false;

View file

@ -37,6 +37,7 @@
#include <QStandardPaths>
#include <QCollator>
#include <QSysInfo>
#include <qrandom.h>
#ifdef Q_OS_UNIX
@ -64,14 +65,13 @@ Q_LOGGING_CATEGORY(lcUtility, "nextcloud.sync.utility", QtInfoMsg)
bool Utility::writeRandomFile(const QString &fname, int size)
{
int maxSize = 10 * 10 * 1024;
qsrand(QDateTime::currentMSecsSinceEpoch());
if (size == -1)
size = qrand() % maxSize;
size = rand() % maxSize;
QString randString;
for (int i = 0; i < size; i++) {
int r = qrand() % 128;
int r = rand() % 128;
randString.append(QChar(r));
}
@ -259,6 +259,11 @@ QString Utility::escape(const QString &in)
return in.toHtmlEscaped();
}
int Utility::rand()
{
return QRandomGenerator::global()->bounded(0, RAND_MAX);
}
void Utility::sleep(int sec)
{
QThread::sleep(sec);

View file

@ -50,6 +50,7 @@ Q_DECLARE_LOGGING_CATEGORY(lcUtility)
* @{
*/
namespace Utility {
OCSYNC_EXPORT int rand();
OCSYNC_EXPORT void sleep(int sec);
OCSYNC_EXPORT void usleep(int usec);
OCSYNC_EXPORT QString formatFingerprint(const QByteArray &, bool colonSeparated = true);

View file

@ -645,7 +645,7 @@ int ConfigFile::updateSegment() const
// Invalid? (Unset at the very first launch)
if(segment < 0 || segment > 99) {
// Save valid segment value, normally has to be done only once.
segment = qrand() % 99;
segment = Utility::rand() % 99;
settings.setValue(QLatin1String(updateSegmentC), segment);
}

View file

@ -63,9 +63,9 @@ QString OWNCLOUDSYNC_EXPORT createDownloadTmpFileName(const QString &previous)
int overhead = 1 + 1 + 2 + 8; // slash dot dot-tilde ffffffff"
int spaceForFileName = qMin(254, tmpFileName.length() + overhead) - overhead;
if (tmpPath.length() > 0) {
return tmpPath + '/' + '.' + tmpFileName.left(spaceForFileName) + ".~" + (QString::number(uint(qrand() % 0xFFFFFFFF), 16));
return tmpPath + '/' + '.' + tmpFileName.left(spaceForFileName) + ".~" + (QString::number(uint(Utility::rand() % 0xFFFFFFFF), 16));
} else {
return '.' + tmpFileName.left(spaceForFileName) + ".~" + (QString::number(uint(qrand() % 0xFFFFFFFF), 16));
return '.' + tmpFileName.left(spaceForFileName) + ".~" + (QString::number(uint(Utility::rand() % 0xFFFFFFFF), 16));
}
}

View file

@ -229,7 +229,7 @@ void PropagateUploadFileNG::slotDeleteJobFinished()
void PropagateUploadFileNG::startNewUpload()
{
ASSERT(propagator()->_activeJobList.count(this) == 1);
_transferId = uint(qrand() ^ uint(_item->_modtime) ^ (uint(_fileToUpload._size) << 16) ^ qHash(_fileToUpload._file));
_transferId = uint(Utility::rand() ^ uint(_item->_modtime) ^ (uint(_fileToUpload._size) << 16) ^ qHash(_fileToUpload._file));
_sent = 0;
_currentChunk = 0;

View file

@ -39,7 +39,7 @@ void PropagateUploadFileV1::doStartUpload()
{
_chunkCount = int(std::ceil(_fileToUpload._size / double(chunkSize())));
_startChunk = 0;
_transferId = uint(qrand()) ^ uint(_item->_modtime) ^ (uint(_fileToUpload._size) << 16);
_transferId = uint(Utility::rand()) ^ uint(_item->_modtime) ^ (uint(_fileToUpload._size) << 16);
const SyncJournalDb::UploadInfo progressInfo = propagator()->_journal->getUploadInfo(_item->_file);

View file

@ -52,10 +52,10 @@ inline QString getFilePathFromUrl(const QUrl &url)
inline QByteArray generateEtag() {
return QByteArray::number(QDateTime::currentDateTimeUtc().toMSecsSinceEpoch(), 16) + QByteArray::number(qrand(), 16);
return QByteArray::number(QDateTime::currentDateTimeUtc().toMSecsSinceEpoch(), 16) + QByteArray::number(OCC::Utility::rand(), 16);
}
inline QByteArray generateFileId() {
return QByteArray::number(qrand(), 16);
return QByteArray::number(OCC::Utility::rand(), 16);
}
class PathComponents : public QStringList {

View file

@ -103,8 +103,8 @@ class TestFolderWatcher : public QObject
#endif
public:
TestFolderWatcher() {
qsrand(QTime::currentTime().msec());
TestFolderWatcher()
{
QDir rootDir(_root.path());
_rootPath = rootDir.canonicalPath();
qDebug() << "creating test directory tree in " << _rootPath;

View file

@ -19,10 +19,9 @@ private:
QString _root;
private slots:
void initTestCase() {
qsrand(QTime::currentTime().msec());
_root = QDir::tempPath() + "/" + "test_" + QString::number(qrand());
void initTestCase()
{
_root = QDir::tempPath() + "/" + "test_" + QString::number(OCC::Utility::rand());
qDebug() << "creating test directory tree in " << _root;
QDir rootDir(_root);
@ -31,7 +30,6 @@ private slots:
rootDir.mkpath(_root + "/a1/b2/c1");
rootDir.mkpath(_root + "/a1/b3/c3");
rootDir.mkpath(_root + "/a2/b3/c3");
}
// Test the recursive path listing function findFoldersBelow

View file

@ -58,8 +58,7 @@ private slots:
void testLaunchOnStartup()
{
qsrand(QDateTime::currentDateTime().toTime_t());
QString postfix = QString::number(qrand());
QString postfix = QString::number(OCC::Utility::rand());
const QString appName = QString::fromLatin1("testLaunchOnStartup.%1").arg(postfix);
const QString guiName = "LaunchOnStartup GUI Name";