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 <edward.welbourne@qt.io>
This commit is contained in:
Edward Welbourne 2024-12-11 15:36:44 +01:00
parent 5d5c8d277a
commit 72519aeb23
2 changed files with 5 additions and 1 deletions

View File

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

View File

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