QCborValue: add an extra check against producing invalid ISO dates

By QCborValue design, we store the textual representation in ISO format,
equivalent of CBOR tag 0, which isn't allowed to have negative years or
beyond year 10000.

Change-Id: Ibdc95e9af7bd456a94ecfffd16060ccff359c296
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Thiago Macieira 2020-04-15 14:00:05 -03:00
parent 52a2505672
commit 2a53017df4
2 changed files with 21 additions and 4 deletions

View File

@ -810,10 +810,12 @@ static QCborValue::Type convertToExtendedType(QCborContainerPrivate *d)
}
if (dt.isValid()) {
QByteArray text = dt.toString(Qt::ISODateWithMs).toLatin1();
replaceByteData(text, text.size(), Element::StringIsAscii);
e.type = QCborValue::String;
d->elements[0].value = qint64(QCborKnownTags::DateTimeString);
return QCborValue::DateTime;
if (!text.isEmpty()) {
replaceByteData(text, text.size(), Element::StringIsAscii);
e.type = QCborValue::String;
d->elements[0].value = qint64(QCborKnownTags::DateTimeString);
return QCborValue::DateTime;
}
}
break;
}

View File

@ -1970,6 +1970,21 @@ void tst_QCborValue::extendedTypeValidation_data()
<< encode(0xc1, 0xfb, -fplimit)
<< QCborValue(QCborKnownTags::UnixTime_t, -fplimit);
}
// But in fact, QCborValue stores date/times as their ISO textual
// representation, which means it can't represent dates before year 1 or
// after year 9999.
{
QDateTime dt(QDate(-1, 1, 1), QTime(0, 0), Qt::UTC);
QTest::newRow("UnixTime_t:negative-year")
<< encode(0xc1, 0x3b, quint64(-dt.toSecsSinceEpoch()) - 1)
<< QCborValue(QCborKnownTags::UnixTime_t, dt.toSecsSinceEpoch());
dt.setDate(QDate(10000, 1, 1));
QTest::newRow("UnixTime_t:year10k")
<< encode(0xc1, 0x1b, quint64(dt.toSecsSinceEpoch()))
<< QCborValue(QCborKnownTags::UnixTime_t, dt.toSecsSinceEpoch());
}
}
void tst_QCborValue::extendedTypeValidation()