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 <marc.mutz@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 585471b138b195cb9020200cae760f119903df85)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Giuseppe D'Angelo 2025-03-14 15:31:48 +01:00 committed by Qt Cherry-pick Bot
parent a6795882d8
commit 06b02f003e
2 changed files with 10 additions and 9 deletions

View File

@ -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));
}

View File

@ -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);