QByteArray: make toDouble() reject space-only strings

This does not affect leading and trailing spaces, which remain allowed.
This is only about a string containing only spaces, which used to be
rejected prior to Qt 5.9 and are rejected with QString (unit tests added
to confirm).

Drive-by indent one QString test row, which I've also reordered so null
comes before empty.

[ChangeLog][QtCore][QByteArray & QByteArrayView] Fixed an old regression
that caused toDouble() and toFloat() to return ok = true for a string
containing only whitespaces.

Pick-to: 6.8
Fixes: QTBUG-137038
Change-Id: Ia6f7714c5e289317de60fffd0f8aa6d2198a91ef
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
(cherry picked from commit 61b17127ae50516323d64523e6cfdf524ae8e974)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Thiago Macieira 2025-05-20 11:06:16 -07:00 committed by Qt Cherry-pick Bot
parent 1efb21ba00
commit 4e785f1da5
3 changed files with 16 additions and 2 deletions

View File

@ -4044,7 +4044,8 @@ double QByteArray::toDouble(bool *ok) const
auto QtPrivate::toDouble(QByteArrayView a) noexcept -> ParsedNumber<double>
{
auto r = qt_asciiToDouble(a.data(), a.size(), WhitespacesAllowed);
a = a.trimmed();
auto r = qt_asciiToDouble(a.data(), a.size());
if (r.ok())
return ParsedNumber{r.result};
else

View File

@ -1497,6 +1497,15 @@ template <typename ByteArray> void tst_QByteArrayApiSymmetry::toFloat() const
QCOMPARE(ByteArray().toFloat(&ok), 0.0f);
QVERIFY(!ok);
ok = true;
QCOMPARE(ByteArray("").toFloat(&ok), 0.0f);
QVERIFY(!ok);
ok = true;
QCOMPARE(ByteArray(" ").toFloat(&ok), 0.0f);
QVERIFY(!ok);
ok = true;
QCOMPARE(ByteArray(" ").toFloat(&ok), 0.0f);
QVERIFY(!ok);
@ -1523,6 +1532,8 @@ void tst_QByteArrayApiSymmetry::toDouble_data() const
QTest::newRow("null") << QByteArray() << 0.0 << false;
QTest::newRow("empty") << QByteArray("") << 0.0 << false;
QTest::newRow("space-only") << QByteArray(" ") << 0.0 << false;
QTest::newRow("spaces-only") << QByteArray(" ") << 0.0 << false;
QTest::newRow("decimal") << QByteArray("1.2345") << 1.2345 << true;
QTest::newRow("exponent lowercase") << QByteArray("1.2345e+01") << 12.345 << true;

View File

@ -7573,8 +7573,10 @@ void tst_QString::double_conversion_data()
// The bad...
QTest::newRow("C empty") << u""_s << false << 0.0;
QTest::newRow("C null") << QString() << false << 0.0;
QTest::newRow("C empty") << u""_s << false << 0.0;
QTest::newRow("C space") << u" "_s << false << 0.0;
QTest::newRow("C spaces") << u" "_s << false << 0.0;
QTest::newRow("C .") << u"."_s << false << 0.0;
QTest::newRow("C 1e") << u"1e"_s << false << 0.0;
QTest::newRow("C 1,") << u"1,"_s << false << 0.0;