From 06b02f003e3955b28c2d9bdc7ab250aea703bb7f Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Fri, 14 Mar 2025 15:31:48 +0100 Subject: [PATCH] Fix calls to non-started QElapsedTimer functions It's illegal to call elapsed() or restart() (sic) on a non-started QElapsedTimer, so don't do that. In QNAM code, this has been observed by adding assertions into QElapsedTimer, which then make network tests crash. (While I am unable to run the network testsuite, this is reproducible locally by running tst_QNetworkDiskCache::setCookieHeader). The QTextStream test also calls restart() without a start(), but then it calls elapsed()+start() which is the whole point of restart(), so amend accordingly. Pick-to: 6.8 6.5 Change-Id: I5cfe308f64b631b68b92843b409777a6b9176828 Reviewed-by: Marc Mutz Reviewed-by: Thiago Macieira (cherry picked from commit 585471b138b195cb9020200cae760f119903df85) Reviewed-by: Qt Cherry-pick Bot --- src/network/access/qnetworkreplyhttpimpl.cpp | 9 ++++++--- .../serialization/qtextstream/tst_qtextstream.cpp | 10 ++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/network/access/qnetworkreplyhttpimpl.cpp b/src/network/access/qnetworkreplyhttpimpl.cpp index 5a0cd052782..cf4d5f1f4ff 100644 --- a/src/network/access/qnetworkreplyhttpimpl.cpp +++ b/src/network/access/qnetworkreplyhttpimpl.cpp @@ -1168,7 +1168,8 @@ void QNetworkReplyHttpImplPrivate::replyDownloadData(QByteArray d) emit q->readyRead(); // emit readyRead before downloadProgress in case this will cause events to be // processed and we get into a recursive call (as in QProgressDialog). - if (downloadProgressSignalChoke.elapsed() >= progressSignalInterval + if (downloadProgressSignalChoke.isValid() && + downloadProgressSignalChoke.elapsed() >= progressSignalInterval && (!decompressHelper.isValid() || decompressHelper.isCountingBytes())) { downloadProgressSignalChoke.restart(); emit q->downloadProgress(bytesDownloaded, totalSizeOpt.value_or(-1)); @@ -1501,7 +1502,8 @@ void QNetworkReplyHttpImplPrivate::replyDownloadProgressSlot(qint64 bytesReceive // processed and we get into a recursive call (as in QProgressDialog). if (bytesDownloaded > 0) emit q->readyRead(); - if (downloadProgressSignalChoke.elapsed() >= progressSignalInterval) { + if (downloadProgressSignalChoke.isValid() && + downloadProgressSignalChoke.elapsed() >= progressSignalInterval) { downloadProgressSignalChoke.restart(); emit q->downloadProgress(bytesDownloaded, bytesTotal); } @@ -1938,7 +1940,8 @@ void QNetworkReplyHttpImplPrivate::_q_cacheLoadReadyRead() // This readyRead() goes to the user. The user then may or may not read() anything. emit q->readyRead(); - if (downloadProgressSignalChoke.elapsed() >= progressSignalInterval) { + if (downloadProgressSignalChoke.isValid() && + downloadProgressSignalChoke.elapsed() >= progressSignalInterval) { downloadProgressSignalChoke.restart(); emit q->downloadProgress(bytesDownloaded, totalSizeOpt.value_or(-1)); } diff --git a/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp b/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp index 37aeb3f1d47..71063f39d0f 100644 --- a/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp +++ b/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp @@ -985,7 +985,7 @@ void tst_QTextStream::performance() }; int elapsed[N] = {0, 0, 0}; - stopWatch.restart(); + stopWatch.start(); int nlines1 = 0; QFile file(m_rfc3261FilePath); QVERIFY(file.open(QFile::ReadOnly)); @@ -995,8 +995,7 @@ void tst_QTextStream::performance() file.readLine(); } - elapsed[0] = stopWatch.elapsed(); - stopWatch.restart(); + elapsed[0] = stopWatch.restart(); int nlines2 = 0; QFile file2(m_rfc3261FilePath); @@ -1008,8 +1007,7 @@ void tst_QTextStream::performance() stream.readLine(); } - elapsed[1] = stopWatch.elapsed(); - stopWatch.restart(); + elapsed[1] = stopWatch.restart(); int nlines3 = 0; QFile file3(m_rfc3261FilePath); @@ -1020,7 +1018,7 @@ void tst_QTextStream::performance() while (stream2.readLineInto(&line)) ++nlines3; - elapsed[2] = stopWatch.elapsed(); + elapsed[2] = stopWatch.restart(); QCOMPARE(nlines1, nlines2); QCOMPARE(nlines2, nlines3);