From 577558daf5a2709722cb7980f194cbbc6a8287a3 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Thu, 9 Jul 2020 18:16:13 +0200 Subject: [PATCH] Fix QByteArray::count implementation for longer data The issue has been introduced when refactoring QByteArray::count implementation (see 631127126cc14e7c01cc611532b3256b58785670). Because the last argument of QByteArrayMatcher::indexIn() method has a defult value, it has been compiling without issues, but was being called with incorrect size parameter. Fixed it to pass the length of the input data correctly. Change-Id: Ic9c2f33733131ec17276aa889f2d7ea40ec79b01 Reviewed-by: Alexandru Croitor --- src/corelib/text/qbytearray.cpp | 4 ++-- .../qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index c498aa30a6b..cbcc4e48f7a 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -2523,8 +2523,8 @@ qsizetype QtPrivate::count(QByteArrayView haystack, QByteArrayView needle) noexc qsizetype num = 0; qsizetype i = -1; if (haystack.size() > 500 && needle.size() > 5) { - QByteArrayMatcher matcher(needle.data()); - while ((i = matcher.indexIn(haystack.data(), i + 1)) != -1) + QByteArrayMatcher matcher(needle.data(), needle.size()); + while ((i = matcher.indexIn(haystack.data(), haystack.size(), i + 1)) != -1) ++num; } else { while ((i = haystack.indexOf(needle, i + 1)) != -1) diff --git a/tests/auto/corelib/text/qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp b/tests/auto/corelib/text/qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp index 33fd25cb507..8b517274179 100644 --- a/tests/auto/corelib/text/qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp +++ b/tests/auto/corelib/text/qbytearrayapisymmetry/tst_qbytearrayapisymmetry.cpp @@ -490,6 +490,13 @@ void tst_QByteArrayApiSymmetry::count_data() QTest::addRow("aaa") << QByteArray("aaa") << QByteArray("a") << 3; QTest::addRow("xyzaaaxyz") << QByteArray("xyzaaxyaxyz") << QByteArray("xyz") << 2; + + const int len = 500; + QByteArray longData(len, 'a'); + const QByteArray needle("abcdef"); + longData.insert(0, needle); + longData.insert(len / 2, needle); + QTest::addRow("longInput") << longData << needle << 2; } template