From 72519aeb237a4085aeb6290b0b4088c690fad106 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.9 6.8 6.5 Fixes: QTBUG-132115 Change-Id: Ic6821bd01394d4dba1be1d25806c372800f8176b Reviewed-by: Edward Welbourne --- 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 50ad4370032..9af92941d6b 100644 --- a/src/corelib/time/qdatetimeparser.cpp +++ b/src/corelib/time/qdatetimeparser.cpp @@ -1358,7 +1358,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 0174fff98df..77154cd4c71 100644 --- a/tests/auto/corelib/time/qdate/tst_qdate.cpp +++ b/tests/auto/corelib/time/qdate/tst_qdate.cpp @@ -1497,6 +1497,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();