From 4e785f1da52582f86bc72a789fa2418102aeb569 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 20 May 2025 11:06:16 -0700 Subject: [PATCH] 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 Reviewed-by: Ivan Solovev (cherry picked from commit 61b17127ae50516323d64523e6cfdf524ae8e974) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/text/qbytearray.cpp | 3 ++- .../tst_qbytearrayapisymmetry.cpp | 11 +++++++++++ tests/auto/corelib/text/qstring/tst_qstring.cpp | 4 +++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 10c61d97ff7..8e87c94089b 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -4044,7 +4044,8 @@ double QByteArray::toDouble(bool *ok) const auto QtPrivate::toDouble(QByteArrayView a) noexcept -> ParsedNumber { - 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 diff --git a/tests/auto/corelib/text/qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp b/tests/auto/corelib/text/qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp index f03086342ce..f8343c8806b 100644 --- a/tests/auto/corelib/text/qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp +++ b/tests/auto/corelib/text/qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp @@ -1497,9 +1497,18 @@ template 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); + // NB: floats < 1e-6 are zero as far as QCOMPARE() is concerned ! const char data[] = "0.0000931322574615478515625"; const float expectedValue = 9.31322574615478515625e-5f; @@ -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; diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 73504b901e2..2681f970c91 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -7573,8 +7573,10 @@ void tst_QString::double_conversion_data() // The bad... + QTest::newRow("C null") << QString() << false << 0.0; QTest::newRow("C empty") << u""_s << false << 0.0; - QTest::newRow("C null") << QString() << 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;