From 8a63c9b43841ee3e9a69926d2ccf32c9f242a65b Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 11 Dec 2024 15:36:44 +0100 Subject: [PATCH] Fix assertion failure on parsing Feb 29 in a non-leap year If there's no way to resolve an actual date with the data parsed, then the date-text given is invalid, so don't try to fix it up. Pick-to: 6.5 Fixes: QTBUG-132115 Change-Id: Ic6821bd01394d4dba1be1d25806c372800f8176b Reviewed-by: Edward Welbourne (cherry picked from commit 72519aeb237a4085aeb6290b0b4088c690fad106) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit e2a0ef30da08ed46ea90e2c7aa297c8f8faf0dbb) --- src/corelib/time/qdatetimeparser.cpp | 4 +++- tests/auto/corelib/time/qdate/tst_qdate.cpp | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/corelib/time/qdatetimeparser.cpp b/src/corelib/time/qdatetimeparser.cpp index 1802a02eac4..66c3b4b0fa2 100644 --- a/src/corelib/time/qdatetimeparser.cpp +++ b/src/corelib/time/qdatetimeparser.cpp @@ -1350,7 +1350,9 @@ QDateTimeParser::scanString(const QDateTime &defaultValue, bool fixup) const if (year % 100 != year2digits && (isSet & YearSection2Digits)) { const QDate date = actualDate(isSet, calendar, defaultCenturyStart, year, year2digits, month, day, dayofweek); - if (!(isSet & YearSection)) { + if (!date.isValid()) { + state = Invalid; + } else if (!(isSet & YearSection)) { year = date.year(); } else { conflicts = true; diff --git a/tests/auto/corelib/time/qdate/tst_qdate.cpp b/tests/auto/corelib/time/qdate/tst_qdate.cpp index 459c0ae3b55..f4b4601e59f 100644 --- a/tests/auto/corelib/time/qdate/tst_qdate.cpp +++ b/tests/auto/corelib/time/qdate/tst_qdate.cpp @@ -1461,6 +1461,8 @@ void tst_QDate::fromStringFormat_data() << u"05-00206-21"_s << u"MM-yyyy-dd"_s << 1900 << QDate(); QTest::newRow("5digit year, back") << u"05-21-00206"_s << u"MM-dd-yyyy"_s << 1900 << QDate(); + QTest::newRow("non-leap-feb-29") // QTBUG-132115: should fail but not assert + << u"290215"_s << u"ddMMyy"_s << 1900 << QDate(); QTest::newRow("dash separator, no year at end") << u"05-21-"_s << u"dd-MM-yyyy"_s << 1900 << QDate();