From e7db7a68f6f3fbf79cb66a4c2e3efb70e84f6bf1 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 7 Aug 2024 16:32:12 +0200 Subject: [PATCH] Remove an unfounded assertion, add the test-case that tripped it The date-time parser, unless it hits a fixed-width numeric field that's unfilled, leaves checking the date against its configured range until it's parsed all fields. This means it can be out of range due to a full-width field when it hits an unfilled one and tries to take the range into account when deciding whether to zero-pad or expect more input into the field. So asserting the current partially-parsed datetime is in range at that point is not justified. Thanks to Albert Astals Cid for reporting this fiendish corner case, found by oss-fuzzing of KDE's image plugins, in IRC. Pick-to: 6.7 6.5 6.2 5.15 Change-Id: Id7db7409a0b5afadafb6920a0785a4cec4c80b62 Reviewed-by: Thiago Macieira Reviewed-by: Albert Astals Cid (cherry picked from commit e0a4de17ddb924d9bf222fb3ea2d0526813edea6) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/time/qdatetimeparser.cpp | 5 ++++- tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/corelib/time/qdatetimeparser.cpp b/src/corelib/time/qdatetimeparser.cpp index cce32e7ad22..56381125460 100644 --- a/src/corelib/time/qdatetimeparser.cpp +++ b/src/corelib/time/qdatetimeparser.cpp @@ -2161,7 +2161,10 @@ bool QDateTimeParser::skipToNextSection(int index, const QDateTime ¤t, QSt if (node.type != TimeZoneSection || current.timeSpec() == Qt::OffsetFromUTC) { const QDateTime maximum = getMaximum(); const QDateTime minimum = getMinimum(); - Q_ASSERT(current >= minimum && current <= maximum); + // Range from minimum to maximum might not contain current if an earlier + // field's value was full-width but out of range. In such a case the + // parse is already headed for Invalid, so it doesn't matter that we get + // the wrong range of values for the current field here. QDateTime tmp = current; if (!setDigit(tmp, index, min) || tmp < minimum) diff --git a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp index f9c6afc795c..ce17a562354 100644 --- a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp @@ -3271,6 +3271,8 @@ void tst_QDateTime::fromStringStringFormat_data() << u"2008-10-13 UTC+01:011.50"_s << u"yyyy-MM-dd thh.mm"_s << 1900 << QDateTime(); QTest::newRow("invalid-time-spec:empty") << u"2001-09-15T09:33:01.001 "_s << u"yyyy-MM-ddThh:mm:ss.z t"_s << 1900 << QDateTime(); + QTest::newRow("invalid-month-year