Commit 9b6dfe1b authored by Rakina Zata Amni's avatar Rakina Zata Amni Committed by Commit Bot

Make TextSearcherICU skip results with zero length

In some cases TextSearcherICU might find result with zero length.
Example case from ClusterFuzz:

<script>
function go(){
setup()
}
window.onload=go;
function setup(){
window.find('\u0080')
}
</script>
<hr id='id9' contenteditable='true'</noscript>
>

Bug: 929217
Change-Id: Ic13b3a3595ae2feb83a7d54a2e8e532f7717e33b
Reviewed-on: https://chromium-review.googlesource.com/c/1457798
Commit-Queue: Rakina Zata Amni <rakina@chromium.org>
Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#629894}
parent 18ad3039
......@@ -139,6 +139,7 @@ TextSearcherICU::~TextSearcherICU() {
void TextSearcherICU::SetPattern(const StringView& pattern,
FindOptions options) {
DCHECK_GT(pattern.length(), 0u);
options_ = options;
SetCaseSensitivity(!(options & kCaseInsensitive));
SetPattern(pattern.Characters16(), pattern.length());
......@@ -186,6 +187,12 @@ bool TextSearcherICU::NextMatchResultInternal(MatchResultICU& result) {
result.start = static_cast<wtf_size_t>(match_start);
result.length = usearch_getMatchedLength(searcher_);
// Might be possible to get zero-length result with some Unicode characters
// that shouldn't actually match but is matched by ICU such as \u0080.
if (result.length == 0u) {
result.start = 0;
return false;
}
return true;
}
......
......@@ -99,4 +99,18 @@ TEST(TextSearcherICUTest, FindSubstringWithOffset) {
EXPECT_EQ(offset_result.length, first_result.length);
}
TEST(TextSearcherICUTest, FindControlCharacter) {
TextSearcherICU searcher;
const String& pattern = MakeUTF16(u8"\u0080");
searcher.SetPattern(pattern, 0);
const String& text = MakeUTF16("some text");
searcher.SetText(text.Characters16(), text.length());
MatchResultICU result;
EXPECT_FALSE(searcher.NextMatchResult(result));
EXPECT_EQ(0u, result.start);
EXPECT_EQ(0u, result.length);
}
} // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment