[network/libktorrent] /: Replace rand() with QRandomGenerator

Jack Hill <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 8973c27922133075d3a2ce21ea67af11a543e037 by Jack Hill.
Committed on 18/07/2026 at 13:43.
Pushed by jackh into branch 'master'.

Replace rand() with QRandomGenerator

M  +4    -2    src/mse/encryptedserverauthenticate.cpp
M  +2    -3    src/tracker/udptrackersocket.cpp
M  +4    -1    testlib/dummytorrentcreator.cpp

https://invent.kde.org/network/libktorrent/-/commit/8973c27922133075d3a2ce21ea67af11a543e037

diff --git a/src/mse/encryptedserverauthenticate.cpp b/src/mse/encryptedserverauthenticate.cpp
index 16deb602..40caf9d7 100644
--- a/src/mse/encryptedserverauthenticate.cpp
+++ b/src/mse/encryptedserverauthenticate.cpp
@@ -4,10 +4,12 @@
     SPDX-License-Identifier: GPL-2.0-or-later
 */
 #include "encryptedserverauthenticate.h"
+
+#include <QRandomGenerator>
+
 #include "encryptedpacketsocket.h"
 #include "functions.h"
 #include "rc4encryptor.h"
-#include <cstdlib>
 #include <torrent/globals.h>
 #include <torrent/server.h>
 #include <util/functions.h>
@@ -38,7 +40,7 @@ void EncryptedServerAuthenticate::sendYB()
     yb.toBuffer(tmp, 96);
     //  DumpBigInt("Xb",xb);
     //  DumpBigInt("Yb",yb);
-    sock->sendData(tmp, 96 + rand() % 512);
+    sock->sendData(tmp, 96 + QRandomGenerator::global()->bounded(512));
     // Out() << "Sent YB" << endl;
 }
 
diff --git a/src/tracker/udptrackersocket.cpp b/src/tracker/udptrackersocket.cpp
index b08ff1be..3709be1b 100644
--- a/src/tracker/udptrackersocket.cpp
+++ b/src/tracker/udptrackersocket.cpp
@@ -6,8 +6,7 @@
 #include "udptrackersocket.h"
 #include <KLocalizedString>
 #include <QHostAddress>
-#include <cstdlib>
-#include <ctime>
+#include <QRandomGenerator>
 #include <net/portlist.h>
 #include <net/serversocket.h>
 #include <net/socket.h>
@@ -256,7 +255,7 @@ void UDPTrackerSocket::handleScrape(const bt::Buffer &buf)
 
 Int32 UDPTrackerSocket::newTransactionID()
 {
-    Int32 transaction_id = rand() * time(nullptr);
+    Int32 transaction_id = QRandomGenerator::global()->generate();
     while (d->transactions.contains(transaction_id)) {
         transaction_id++;
     }
diff --git a/testlib/dummytorrentcreator.cpp b/testlib/dummytorrentcreator.cpp
index ec337c29..ab13573b 100644
--- a/testlib/dummytorrentcreator.cpp
+++ b/testlib/dummytorrentcreator.cpp
@@ -5,7 +5,10 @@
 */
 
 #include "dummytorrentcreator.h"
+
 #include <QFile>
+#include <QRandomGenerator>
+
 #include <torrent/torrentcreator.h>
 #include <util/error.h>
 #include <util/fileops.h>
@@ -93,7 +96,7 @@ bool DummyTorrentCreator::createRandomFile(const QString &path, bt::Uint64 size)
     while (written < size) {
         char tmp[4096];
         for (int i = 0; i < 4096; i++) {
-            tmp[i] = rand() % 0xFF;
+            tmp[i] = QRandomGenerator::global()->bounded(0xFF);
         }
 
         bt::Uint64 to_write = 4096;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.