diff --git a/src/cmd/cmd.cpp b/src/cmd/cmd.cpp index 191dcc547..c7cea418c 100644 --- a/src/cmd/cmd.cpp +++ b/src/cmd/cmd.cpp @@ -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; diff --git a/src/common/utility.cpp b/src/common/utility.cpp index e31534ca3..bd9394b8c 100644 --- a/src/common/utility.cpp +++ b/src/common/utility.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #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); diff --git a/src/common/utility.h b/src/common/utility.h index 0d0bc3ade..9522d188e 100644 --- a/src/common/utility.h +++ b/src/common/utility.h @@ -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); diff --git a/src/libsync/configfile.cpp b/src/libsync/configfile.cpp index fddd5ed88..0f2059be2 100644 --- a/src/libsync/configfile.cpp +++ b/src/libsync/configfile.cpp @@ -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); } diff --git a/src/libsync/propagatedownload.cpp b/src/libsync/propagatedownload.cpp index d55f4f1ee..cdda9abeb 100644 --- a/src/libsync/propagatedownload.cpp +++ b/src/libsync/propagatedownload.cpp @@ -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)); } } diff --git a/src/libsync/propagateuploadng.cpp b/src/libsync/propagateuploadng.cpp index b99fc06cc..0b4b40a86 100644 --- a/src/libsync/propagateuploadng.cpp +++ b/src/libsync/propagateuploadng.cpp @@ -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; diff --git a/src/libsync/propagateuploadv1.cpp b/src/libsync/propagateuploadv1.cpp index 8e4ec1e05..40662fa20 100644 --- a/src/libsync/propagateuploadv1.cpp +++ b/src/libsync/propagateuploadv1.cpp @@ -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); diff --git a/test/syncenginetestutils.h b/test/syncenginetestutils.h index 7596bbd90..6b170866a 100644 --- a/test/syncenginetestutils.h +++ b/test/syncenginetestutils.h @@ -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 { diff --git a/test/testfolderwatcher.cpp b/test/testfolderwatcher.cpp index 7345e6450..d8c7da092 100644 --- a/test/testfolderwatcher.cpp +++ b/test/testfolderwatcher.cpp @@ -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; diff --git a/test/testinotifywatcher.cpp b/test/testinotifywatcher.cpp index 2d8cf937d..204939f91 100644 --- a/test/testinotifywatcher.cpp +++ b/test/testinotifywatcher.cpp @@ -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 diff --git a/test/testutility.cpp b/test/testutility.cpp index cd0d1045c..f65bf985e 100644 --- a/test/testutility.cpp +++ b/test/testutility.cpp @@ -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";