From 4c6dfc05df4330c787bbb2a1b657dbcc944ca4ea Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Fri, 15 Sep 2023 17:30:43 +0300 Subject: [PATCH] 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 --- src/corelib/text/qlatin1stringmatcher.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/corelib/text/qlatin1stringmatcher.cpp b/src/corelib/text/qlatin1stringmatcher.cpp index e23b4f20f3..68bf97db5c 100644 --- a/src/corelib/text/qlatin1stringmatcher.cpp +++ b/src/corelib/text/qlatin1stringmatcher.cpp @@ -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;