Skip to content

Commit

Permalink
Resolve WebCore::findWordBoundary crash
Browse files Browse the repository at this point in the history
rdar://128246589
https://bugs.webkit.org/show_bug.cgi?id=273907

Reviewed by Wenson Hsieh.

Fix empty string case to resolve findWordBoundary crash

Combined changes:
* Source/WebCore/platform/text/mac/TextBoundaries.mm:
(WebCore::findWordBoundary):

Canonical link: https://commits.webkit.org/279076@main
  • Loading branch information
abigailfox committed May 21, 2024
1 parent 7430650 commit d8b017d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Source/WebCore/platform/text/mac/TextBoundaries.mm
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,15 @@ void findWordBoundary(StringView text, int position, int* start, int* end)
*start = range.location;
*end = range.location + range.length;
#else
if (text.isEmpty()) {
*start = 0;
*end = 0;
return;
}

unsigned pos = position;
if (pos == text.length() && pos)
--pos;
if (pos >= text.length() && pos)
pos = text.length() - 1;

// For complex text (Thai, Japanese, Chinese), visible_units will pass the text in as a
// single contiguous run of characters, providing as much context as is possible.
Expand Down
4 changes: 4 additions & 0 deletions Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
11C2598D21FA6324004C9E23 /* async-script-load.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 11C2598C21FA618D004C9E23 /* async-script-load.html */; };
143DDE9820C9018B007F76FA /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 574F55D0204D471C002948C6 /* Security.framework */; };
14CC42E624B8D8FA00E64F48 /* JSRunLoopTimer.mm in Sources */ = {isa = PBXBuildFile; fileRef = 14CC42E524B8D8FA00E64F48 /* JSRunLoopTimer.mm */; };
155B87332BF55D7800D93968 /* TextBoundaries.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 155B872B2BF55D7800D93968 /* TextBoundaries.cpp */; };
1A02C870125D4CFD00E3F4BD /* find.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 1A02C84B125D4A5E00E3F4BD /* find.html */; };
1A3524AE1D63A4FB0031729B /* Scope.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A3524AC1D63A4FB0031729B /* Scope.cpp */; };
1A4F81CF1BDFFD53004E672E /* RemoteObjectRegistryPlugIn.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1A4F81CD1BDFFD53004E672E /* RemoteObjectRegistryPlugIn.mm */; };
Expand Down Expand Up @@ -2110,6 +2111,7 @@
144D40EC221B46A7004B474F /* UUID.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UUID.cpp; sourceTree = "<group>"; };
14CC42E524B8D8FA00E64F48 /* JSRunLoopTimer.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = JSRunLoopTimer.mm; sourceTree = "<group>"; };
14F3B11215E45EAB00210069 /* SaturatedArithmeticOperations.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SaturatedArithmeticOperations.cpp; sourceTree = "<group>"; };
155B872B2BF55D7800D93968 /* TextBoundaries.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TextBoundaries.cpp; sourceTree = "<group>"; };
1A02C84B125D4A5E00E3F4BD /* find.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = find.html; sourceTree = "<group>"; };
1A02C84E125D4A8400E3F4BD /* Find.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Find.cpp; sourceTree = "<group>"; };
1A3524A91D627BD40031729B /* DeletedAddressOfOperator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DeletedAddressOfOperator.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4524,6 +4526,7 @@
CE4D5DE51F6743BA0072CFC6 /* StringWithDirection.cpp */,
41E67A8425D16E83007B0A4C /* STUNMessageParsingTest.cpp */,
1C9DD9222697655B00274DB2 /* SVGImageCasts.cpp */,
155B872B2BF55D7800D93968 /* TextBoundaries.cpp */,
93A258981F92FF15003E510C /* TextCodec.cpp */,
CDC2C7141797089D00E627FB /* TimeRanges.cpp */,
7AD3FE8D1D75FB8D00B169A4 /* TransformationMatrix.cpp */,
Expand Down Expand Up @@ -6857,6 +6860,7 @@
F4517B672054C49500C26721 /* TestWKWebViewController.mm in Sources */,
F472E00327D966F200F3A172 /* TextAlternatives.mm in Sources */,
F45033F5206BEC95009351CE /* TextAutosizingBoost.mm in Sources */,
155B87332BF55D7800D93968 /* TextBoundaries.cpp in Sources */,
93E6193B1F931B3A00AF245E /* TextCodec.cpp in Sources */,
CE3524F91B1441C40028A7C5 /* TextFieldDidBeginAndEndEditing.cpp in Sources */,
F494B36A263120780060A310 /* TextServicesTests.mm in Sources */,
Expand Down
43 changes: 43 additions & 0 deletions Tools/TestWebKitAPI/Tests/WebCore/TextBoundaries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"
#include <WebCore/TextBoundaries.h>

namespace TestWebKitAPI {

TEST(TextBoundariesTest, FindWordBoundaryEmpty)
{
WTF::String emptyString(""_s);
WTF::StringView stringView(emptyString);
int start = -1;
int end = -1;

WebCore::findWordBoundary(stringView, 0, &start, &end);

ASSERT(!start);
ASSERT(!end);
}
}

0 comments on commit d8b017d

Please sign in to comment.