Use QRandomGenerator instead of qrand
authorFelix Weilbach <felix.weilbach@nextcloud.com>
Fri, 3 Sep 2021 19:37:46 +0000 (21:37 +0200)
committerFelix Weilbach (Rebase PR Action) <felix.weilbach@t-online.de>
Wed, 8 Sep 2021 08:54:00 +0000 (08:54 +0000)
Signed-off-by: Felix Weilbach <felix.weilbach@nextcloud.com>
src/cmd/cmd.cpp
src/common/utility.cpp
src/common/utility.h
src/libsync/configfile.cpp
src/libsync/propagatedownload.cpp
src/libsync/propagateuploadng.cpp
src/libsync/propagateuploadv1.cpp
test/syncenginetestutils.h
test/testfolderwatcher.cpp
test/testinotifywatcher.cpp
test/testutility.cpp

index 191dcc547b08b4be9a786ecfb861ac820f18f193..c7cea418c415d516d506bf83ae3868ff24317ec6 100644 (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;
index e31534ca3808e1e95c4d652309d5728452e6a77a..bd9394b8c3e6e32eae57886ed904191986637a69 100644 (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);
index 0d0bc3ade383085d7dee944e98c9d9cf328793bd..9522d188e7ddd4afd1dc4c33d372544dc5ee4f1e 100644 (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);
index fddd5ed88e6587ce3ece97147077586619eb7839..0f2059be2669220de494dbfcf56fff2eba702d8c 100644 (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);
     }
 
index d55f4f1eeb10c88b1259536d1514dd3451d6c160..cdda9abeb0cb7b87c8da1923a164ae48d5009ce1 100644 (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));
     }
 }
 
index b99fc06cce444b97bff3f30058700d8c9e537cd7..0b4b40a861e573ee1d7cd366805b3b7057f89862 100644 (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;
 
index 8e4ec1e05e6e023166653cab3d87bbab058de5f0..40662fa20792156a98502c1846e816d1c697a416 100644 (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);
 
index 7596bbd904f6d7053984ed364af372df11016842..6b170866aa4cc3a657b21a18a2a0d4c041cffadd 100644 (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 {
index 7345e645025deffe80f572caa8a2936b271244ee..d8c7da0924f7ff3b09567ec2e995abed8aa1e307 100644 (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;
index 2d8cf937d10bc096676e628fcbc15ef6de66d7a1..204939f9132c66d873f5de0583cb74170c5847f6 100644 (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
index cd0d1045c99f7f5c11fea0f42c1e8802c6ee47bc..f65bf985ec79a8f5c40cf083632d89813cbbcae4 100644 (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";