From 612b67cf13cedb832e082308b620f948377ddf21 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Thu, 8 Feb 2024 17:19:26 +0100 Subject: QTimer: do not set active state when setting a negative interval QObject::startTimer() returns 0 in case of failure, for example when someone tries to register a timer with a negative interval. However, QTimer internally uses -1 as an invalid timer id. This could lead to a situation when the timer was not really started, but QTimer::isActive() returned true. This patch fixes it in two ways: - check the return value of QObject::startTimer() and treat 0 as an error. - do not treat 0 as a valid timer id when calculating the active state. As a drive-by: move the `using namespace std::chrono_literals;` declaration to the top of tst_qtimer.cpp, so that we do not need to repeat it in each test case. Fixes: QTBUG-122087 Pick-to: 6.7 6.6 6.5 Change-Id: I0e21152b2173ebb5fb0dada1b99a903a321ca9c4 Reviewed-by: Ahmad Samir --- tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp | 38 ++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'tests/auto/corelib/kernel/qtimer') diff --git a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp index 18ffbe2908..c318c3a625 100644 --- a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp +++ b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp @@ -39,6 +39,8 @@ static bool glibDisabled = []() { }(); #endif +using namespace std::chrono_literals; + class tst_QTimer : public QObject { Q_OBJECT @@ -94,6 +96,8 @@ private slots: void bindToTimer(); void bindTimer(); void automatedBindingTests(); + + void negativeInterval(); }; void tst_QTimer::zeroTimer() @@ -166,7 +170,6 @@ void tst_QTimer::singleShotNormalizes_data() void tst_QTimer::singleShotNormalizes() { - using namespace std::chrono_literals; static constexpr auto TestTimeout = 250ms; QFETCH(QByteArray, slotName); QEventLoop loop; @@ -1287,6 +1290,16 @@ void tst_QTimer::bindToTimer() timer.stop(); QVERIFY(!active); + + // also test that using negative interval updates the binding correctly + timer.start(100); + QVERIFY(active); + timer.setInterval(-100); + QVERIFY(!active); + timer.start(100); + QVERIFY(active); + timer.start(-100); + QVERIFY(!active); } void tst_QTimer::bindTimer() @@ -1367,6 +1380,29 @@ void tst_QTimer::automatedBindingTests() } } +void tst_QTimer::negativeInterval() +{ + QTimer timer; + + // Starting with a negative interval does not change active state. + timer.start(-100ms); + QVERIFY(!timer.isActive()); + + // Updating the interval to a negative value stops the timer and changes + // the active state. + timer.start(100ms); + QVERIFY(timer.isActive()); + timer.setInterval(-100); + QVERIFY(!timer.isActive()); + + // Starting with a negative interval when already started leads to stop + // and inactive state. + timer.start(100); + QVERIFY(timer.isActive()); + timer.start(-100ms); + QVERIFY(!timer.isActive()); +} + class OrderHelper : public QObject { Q_OBJECT -- cgit v1.2.3