diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2021-07-22 16:52:40 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-07-29 13:00:45 +0000 |
commit | 69461206c999f03583c9fc2251212462cd0401ce (patch) | |
tree | 614ada97ca948b7c2f7e643e15e22bb36b6a166f /tests/auto/corelib/text | |
parent | 3267f4e9fcd5cb3b0637624157fbdcd262f377c7 (diff) |
QByteArray: fix indexOf/lastIndexOf
This patch fixed two bugs in indexOf/lastIndexOf:
1. The lastIndexOf(char, qsizetype) overload was crashing with an empty
QByteArray. It was unconditionally calling lastIndexOfCharHelper()
which assumes that this QBA is not empty. An explicit check for
the empty case is added.
2. The indexOf(QByteArray, qsizetype) overload was behaving incorrectly
while searching for an empty QByteArray. In this case it
unconditionally returned its second parameter (from). However, from
can be negative, or even exceed the size of this QByteArray. This
patch handles this cases properly.
As a drive-by: this patch adjusts the QByteArray::indexOf(char, qsizetype)
and QByteArray::lastIndexOf(char, qsizetype) overloads to match with the
QByteArrayView implementation. This is done to have similar code paths
in both cases and avoid tricky bugs in future.
Ideally we had to adjust the QByteArrayView implementation, but it's
fully inline, so can't be changed without breaking BC.
Task-number: QTBUG-91736
Change-Id: Iaef2fdc5b99cce6aa342cca2d17544a1ad7ca677
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
(cherry picked from commit e150bcfe4d5514b2a2960234049c514bc558adee)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests/auto/corelib/text')
-rw-r--r-- | tests/auto/corelib/text/qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/tests/auto/corelib/text/qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp b/tests/auto/corelib/text/qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp index 8b51727417..40d7291e41 100644 --- a/tests/auto/corelib/text/qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp +++ b/tests/auto/corelib/text/qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp @@ -316,12 +316,20 @@ void tst_QByteArrayApiSymmetry::indexOf_data() static const char n19[] = { 0x00, 0x00, 0x01, 0x00 }; QTest::newRow("19") << QByteArray(h19, sizeof(h19)) << QByteArray(n19, sizeof(n19)) << 0 << -1; - QTest::newRow("empty") << QByteArray("") << QByteArray("x") << 0 << -1; - QTest::newRow("null") << QByteArray() << QByteArray("x") << 0 << -1; + QTest::newRow("empty from 0") << QByteArray("") << QByteArray("x") << 0 << -1; + QTest::newRow("empty from -1") << QByteArray("") << QByteArray("x") << -1 << -1; + QTest::newRow("empty from 1") << QByteArray("") << QByteArray("x") << 1 << -1; + QTest::newRow("null from 0") << QByteArray() << QByteArray("x") << 0 << -1; + QTest::newRow("null from -1") << QByteArray() << QByteArray("x") << -1 << -1; + QTest::newRow("null from 1") << QByteArray() << QByteArray("x") << 1 << -1; QTest::newRow("null-in-null") << QByteArray() << QByteArray() << 0 << 0; QTest::newRow("empty-in-null") << QByteArray() << QByteArray("") << 0 << 0; QTest::newRow("null-in-empty") << QByteArray("") << QByteArray() << 0 << 0; QTest::newRow("empty-in-empty") << QByteArray("") << QByteArray("") << 0 << 0; + QTest::newRow("empty in abc from 0") << abc << QByteArray() << 0 << 0; + QTest::newRow("empty in abc from 2") << abc << QByteArray() << 2 << 2; + QTest::newRow("empty in abc from 5") << abc << QByteArray() << 5 << -1; + QTest::newRow("empty in abc from -1") << abc << QByteArray() << -1 << 2; QByteArray veryBigHaystack(500, 'a'); veryBigHaystack += 'B'; @@ -404,12 +412,23 @@ void tst_QByteArrayApiSymmetry::lastIndexOf_data() static const char n25[] = { 0x00, 0x00, 0x01, 0x00 }; QTest::newRow("25") << QByteArray(h25, sizeof(h25)) << QByteArray(n25, sizeof(n25)) << 0 << -1; - QTest::newRow("empty") << QByteArray("") << QByteArray("x") << -1 << -1; - QTest::newRow("null") << QByteArray() << QByteArray("x") << -1 << -1; + QTest::newRow("empty from 0") << QByteArray("") << QByteArray("x") << 0 << -1; + QTest::newRow("empty from -1") << QByteArray("") << QByteArray("x") << -1 << -1; + QTest::newRow("empty from 1") << QByteArray("") << QByteArray("x") << 1 << -1; + QTest::newRow("null from 0") << QByteArray() << QByteArray("x") << 0 << -1; + QTest::newRow("null from -1") << QByteArray() << QByteArray("x") << -1 << -1; + QTest::newRow("null from 1") << QByteArray() << QByteArray("x") << 1 << -1; QTest::newRow("null-in-null") << QByteArray() << QByteArray() << -1 << 0; QTest::newRow("empty-in-null") << QByteArray() << QByteArray("") << -1 << 0; QTest::newRow("null-in-empty") << QByteArray("") << QByteArray() << -1 << 0; QTest::newRow("empty-in-empty") << QByteArray("") << QByteArray("") << -1 << 0; + QTest::newRow("empty in abc from 0") << abc << QByteArray() << 0 << 0; + QTest::newRow("empty in abc from 2") << abc << QByteArray() << 2 << 2; + QTest::newRow("empty in abc from 5") + << abc << QByteArray() << 5 << -1; // perversely enough, should be 3? + QTest::newRow("empty in abc from -1") << abc << QByteArray() << -1 << 3; + QTest::newRow("empty in abc from -5") + << abc << QByteArray() << -5 << 3; // perversely enough, should be -1? } template<typename Haystack, typename Needle> |