Bug 1812386 - Limit lookahead length in mozTXTToHTMLConv::NumberOfMatches to mitigate O(n^2) performance degradation. r=necko-reviewers,valentin

Arbitrarily limit how far we'll look ahead for matching delimiters,
to avoid hanging on huge strings.
This could result in failing to style a chunk of the content,
but it's unlikely any resulting styling would be useful anyhow.

Based on https://github.com/Betterbird/thunderbird-patches/blob/main/102/bugs/1812386-optimise-FindURLStart-End-m-c.patch

Differential Revision: https://phabricator.services.mozilla.com/D171720
This commit is contained in:
Jonathan Kew 2023-03-06 16:02:59 +00:00
parent 3775a087cc
commit 3002762e41

View File

@ -597,7 +597,11 @@ uint32_t mozTXTToHTMLConv::NumberOfMatches(const char16_t* aInString,
LIMTYPE before, LIMTYPE after) {
uint32_t result = 0;
const uint32_t len = mozilla::AssertedCast<uint32_t>(aInStringLength);
// Limit lookahead length to avoid pathological O(n^2) behavior; looking so
// far ahead is unlikely to be important for cases where styling marked-up
// fragments is actually useful anyhow.
const uint32_t len =
std::min(2000u, mozilla::AssertedCast<uint32_t>(aInStringLength));
GraphemeClusterBreakIteratorUtf16 ci(Span<const char16_t>(aInString, len));
for (uint32_t pos = 0; pos < len; pos = *ci.Next()) {
if (ItMatchesDelimited(aInString + pos, aInStringLength - pos, rep, aRepLen,