summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2021-07-22 16:52:40 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-07-29 13:00:45 +0000
commit69461206c999f03583c9fc2251212462cd0401ce (patch)
tree614ada97ca948b7c2f7e643e15e22bb36b6a166f /src
parent3267f4e9fcd5cb3b0637624157fbdcd262f377c7 (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 'src')
-rw-r--r--src/corelib/text/qbytearray.cpp15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp
index a710c6ecba..a99292f5e0 100644
--- a/src/corelib/text/qbytearray.cpp
+++ b/src/corelib/text/qbytearray.cpp
@@ -2421,12 +2421,17 @@ static inline qsizetype findCharHelper(QByteArrayView haystack, qsizetype from,
qsizetype QtPrivate::findByteArray(QByteArrayView haystack, qsizetype from, QByteArrayView needle) noexcept
{
const auto ol = needle.size();
- if (ol == 0)
- return from;
+ const auto l = haystack.size();
+ if (ol == 0) {
+ if (from < 0)
+ return qMax(from + l, 0);
+ else
+ return from > l ? -1 : from;
+ }
+
if (ol == 1)
return findCharHelper(haystack, from, needle.front());
- const auto l = haystack.size();
if (from > l || ol + from > l)
return -1;
@@ -2461,7 +2466,7 @@ qsizetype QtPrivate::findByteArray(QByteArrayView haystack, qsizetype from, QByt
qsizetype QByteArray::indexOf(char ch, qsizetype from) const
{
- return static_cast<int>(findCharHelper(*this, from, ch));
+ return qToByteArrayViewIgnoringNull(*this).indexOf(ch, from);
}
static qsizetype lastIndexOfHelper(const char *haystack, qsizetype l, const char *needle,
@@ -2556,7 +2561,7 @@ qsizetype QtPrivate::lastIndexOf(QByteArrayView haystack, qsizetype from, QByteA
qsizetype QByteArray::lastIndexOf(char ch, qsizetype from) const
{
- return static_cast<int>(lastIndexOfCharHelper(*this, from, ch));
+ return qToByteArrayViewIgnoringNull(*this).lastIndexOf(ch, from);
}
static inline qsizetype countCharHelper(QByteArrayView haystack, char needle) noexcept