QLatin1StringMatcher: handle from index < 0

indexIn() could hit UB if `from` < 0, so handle that case too. (This
also makes using noexcept for that method more correct wrt the Lakos
rule). While I think that passing valid indexes to a method is the
responsibility of the caller and that the existing API in Qt where a
negative index is interpretted as "search from the end" is
hard-to-use/bad API, not handling `from < 0` this way will most likely
not pass review (c.f. commit 81538c5219b92356c52233a80845847145795d6f on
gerrit). I'd rather get this over with so that an indexIn(QSV) overload
can be added...

Change-Id: Id85d18c901c82d9652804ecff7bfe7c71c10a63c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2023-09-15 17:30:43 +03:00
parent 04f4b27774
commit 4c6dfc05df

View File

@ -162,8 +162,11 @@ qsizetype QLatin1StringMatcher::indexIn(QLatin1StringView haystack, qsizetype fr
{
if (m_pattern.isEmpty() && from == haystack.size())
return from;
if (from < 0) // Historical behavior (see QString::indexOf and co.)
from += haystack.size();
if (from >= haystack.size())
return -1;
auto begin = haystack.begin() + from;
auto end = haystack.end();
auto found = begin;