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 <edward.welbourne@qt.io>
(cherry picked from commit 72519aeb237a4085aeb6290b0b4088c690fad106)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit e2a0ef30da08ed46ea90e2c7aa297c8f8faf0dbb)
This commit is contained in:
Edward Welbourne 2024-12-11 15:36:44 +01:00 committed by Qt Cherry-pick Bot
parent 6a9343fa15
commit 8a63c9b438
2 changed files with 5 additions and 1 deletions

View File

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

View File

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