Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CrashTracer: com.apple.WebKit.WebContent at com.apple.WebKit: WebKit::TextCheckingControllerProxy::annotatedSubstringBetweenPositions #681

Closed
wants to merge 1 commit into from

Conversation

dcrousso
Copy link
Member

@dcrousso dcrousso commented May 17, 2022

0cc930a

CrashTracer: com.apple.WebKit.WebContent at com.apple.WebKit: WebKit::TextCheckingControllerProxy::annotatedSubstringBetweenPositions
https://bugs.webkit.org/show_bug.cgi?id=239909
<rdar://problem/87885717 >

Reviewed by Wenson Hsieh.

This exception happens when trying to add attributes for text that contains collapsed whitespace and
has also been wrapped due to the size of its parent container. The exception specifically is about
trying to add attributes beyond the current length of a `NSAttributedString`.

* Source/WebCore/editing/TextIteratorBehavior.h:
* Source/WebCore/editing/TextIterator.cpp:
(WebCore::TextIterator::handleTextRun):
In the case that `m_lastTextNodeEndedWithCollapsedSpace`, we only want to add the remaining text if
we're still within the desired portion of the `m_textRun`. Otherwise, we'll iterate over too much of
the text and result in a string that's longer than what would be the case if one manually calculated
it from the given `offset` and `offsetEnd`. Add a new `TextIteratorBehavior::IgnoresWhiteSpaceAtEndOfRun`
to not include the trailing whitespace.

* Source/WebKit/WebProcess/WebPage/Cocoa/TextCheckingControllerProxy.mm:
(WebKit::TextCheckingControllerProxy::annotatedSubstringBetweenPositions):
Use the new `WebCore::TextIteratorBehavior::IgnoresWhiteSpaceAtEndOfRun` to not include the trailing
whitespace.
Also add some defensive checks just in case.

* Source/WebCore/testing/Internals.idl:
* Source/WebCore/testing/Internals.h:
* Source/WebCore/testing/Internals.cpp:
(WebCore::toTextIteratorBehaviors): Added.
(WebCore::Internals::locationFromRange):
(WebCore::Internals::lengthFromRange):
(WebCore::Internals::statesOfTextIterator): Added.
Add a way to provide `TextIteratorBehaviors` to methods that use `TextIterator`.
Add a method that gets the `text` and `range` of a `TextIterator` after every `advance`.

* LayoutTests/editing/text-iterator/sequential-collapsed-ranges.html: Added.
* LayoutTests/editing/text-iterator/sequential-collapsed-ranges-expected.txt: Added.
* LayoutTests/editing/text-iterator/subrange-with-trailing-collapsed-whitespace.html: Added.
* LayoutTests/editing/text-iterator/subrange-with-trailing-collapsed-whitespace-expected.txt: Added.

Canonical link: https://commits.webkit.org/251061@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@294955 268f45cc-cd09-0410-ab3c-d52691b4dbfc

@dcrousso dcrousso self-assigned this May 17, 2022
@dcrousso dcrousso added HTML Editing For bugs in HTML editing support (including designMode and contentEditable). WebKit Nightly Build labels May 17, 2022
@webkit-early-warning-system webkit-early-warning-system added the merging-blocked Applied to prevent a change from being merged label May 17, 2022
@rniwa
Copy link
Member

rniwa commented May 17, 2022

This exception happens when trying to add attributes for text that contains collapsed whitespace and has also been wrapped due to the size of its parent container. The exception specifically is about trying to add attributes beyond the current length of a NSAttributedString.

In the case that m_lastTextNodeEndedWithCollapsedSpace, we only want to add the remaining text if we're still within the desired portion of the m_textRun. Otherwise, we'll iterate over too much of the text and result in a string that's longer than what would be the case if one manually calculated it from the given offset and offsetEnd. Add a new TextIteratorBehavior::IgnoresWhiteSpaceAtEndOfRun to not include the trailing whitespace.

Please hard wrap these long lines.

// Check for collapsed space at the start of this run.
bool needSpace = m_lastTextNodeEndedWithCollapsedSpace || (m_textRun == firstTextRun && textRunStart == runStart && runStart);
if (needSpace && !renderer.style().isCollapsibleWhiteSpace(m_lastCharacter) && m_lastCharacter) {
if (runStart >= runEnd && m_behaviors.contains(TextIteratorBehavior::IgnoresWhiteSpaceAtEndOfRun)) {
m_textRun = nextTextRun;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the next text run was also entirely consisting of whitespace?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand your question 😅

The idea behind this code is to do similar to what's done on :688 in that if we've reached the end of the desired range (i.e. runStart >= runEnd) then we should stop processing things and instead just keep iterating until we run out of text runs to iterate.

When I first read this code it just struck me as odd that we didn't check runStart < runEnd for the collapsed whitespace case. My gut is telling me that this should always be the behavior in all cases (i.e. we shouldn't be reading text beyond the end of the range we want), but I created TextIteratorBehavior::IgnoresWhiteSpaceAtEndOfRun to minimize any risk of other issues (since this is a fix for a crash, i wouldn't want the change to have to get rolled out because it caused some other problem).

Copy link
Member Author

@dcrousso dcrousso May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i added some tests for this (assuming i understood your point 😅)

AFAICT TextIteratorBehavior::IgnoresWhiteSpaceAtEndOfRun does not affect multiple whitespace text runs in a row

@dcrousso dcrousso removed merging-blocked Applied to prevent a change from being merged HTML Editing For bugs in HTML editing support (including designMode and contentEditable). WebKit Nightly Build labels May 18, 2022
@dcrousso dcrousso added HTML Editing For bugs in HTML editing support (including designMode and contentEditable). WebKit Nightly Build labels May 18, 2022
@webkit-early-warning-system webkit-early-warning-system added the merging-blocked Applied to prevent a change from being merged label May 21, 2022
@dcrousso dcrousso removed merging-blocked Applied to prevent a change from being merged HTML Editing For bugs in HTML editing support (including designMode and contentEditable). WebKit Nightly Build labels May 25, 2022
@dcrousso dcrousso added HTML Editing For bugs in HTML editing support (including designMode and contentEditable). WebKit Nightly Build labels May 25, 2022
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For new tests, we generally prefer using js-test.js instead of the pre/post scripts.

<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Ditto re: pre/post.js)

@webkit-early-warning-system webkit-early-warning-system added the merging-blocked Applied to prevent a change from being merged label May 26, 2022
@dcrousso dcrousso removed merging-blocked Applied to prevent a change from being merged HTML Editing For bugs in HTML editing support (including designMode and contentEditable). WebKit Nightly Build labels May 27, 2022
@dcrousso dcrousso added the HTML Editing For bugs in HTML editing support (including designMode and contentEditable). label May 27, 2022
@webkit-early-warning-system webkit-early-warning-system added the merging-blocked Applied to prevent a change from being merged label May 27, 2022
@dcrousso dcrousso added merge-queue Applied to send a pull request to merge-queue and removed merging-blocked Applied to prevent a change from being merged labels May 27, 2022
…:TextCheckingControllerProxy::annotatedSubstringBetweenPositions

https://bugs.webkit.org/show_bug.cgi?id=239909
<rdar://problem/87885717>

Reviewed by Wenson Hsieh.

This exception happens when trying to add attributes for text that contains collapsed whitespace and
has also been wrapped due to the size of its parent container. The exception specifically is about
trying to add attributes beyond the current length of a `NSAttributedString`.

* Source/WebCore/editing/TextIteratorBehavior.h:
* Source/WebCore/editing/TextIterator.cpp:
(WebCore::TextIterator::handleTextRun):
In the case that `m_lastTextNodeEndedWithCollapsedSpace`, we only want to add the remaining text if
we're still within the desired portion of the `m_textRun`. Otherwise, we'll iterate over too much of
the text and result in a string that's longer than what would be the case if one manually calculated
it from the given `offset` and `offsetEnd`. Add a new `TextIteratorBehavior::IgnoresWhiteSpaceAtEndOfRun`
to not include the trailing whitespace.

* Source/WebKit/WebProcess/WebPage/Cocoa/TextCheckingControllerProxy.mm:
(WebKit::TextCheckingControllerProxy::annotatedSubstringBetweenPositions):
Use the new `WebCore::TextIteratorBehavior::IgnoresWhiteSpaceAtEndOfRun` to not include the trailing
whitespace.
Also add some defensive checks just in case.

* Source/WebCore/testing/Internals.idl:
* Source/WebCore/testing/Internals.h:
* Source/WebCore/testing/Internals.cpp:
(WebCore::toTextIteratorBehaviors): Added.
(WebCore::Internals::locationFromRange):
(WebCore::Internals::lengthFromRange):
(WebCore::Internals::statesOfTextIterator): Added.
Add a way to provide `TextIteratorBehaviors` to methods that use `TextIterator`.
Add a method that gets the `text` and `range` of a `TextIterator` after every `advance`.

* LayoutTests/editing/text-iterator/sequential-collapsed-ranges.html: Added.
* LayoutTests/editing/text-iterator/sequential-collapsed-ranges-expected.txt: Added.
* LayoutTests/editing/text-iterator/subrange-with-trailing-collapsed-whitespace.html: Added.
* LayoutTests/editing/text-iterator/subrange-with-trailing-collapsed-whitespace-expected.txt: Added.

Canonical link: https://commits.webkit.org/251061@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@294955 268f45cc-cd09-0410-ab3c-d52691b4dbfc
@webkit-early-warning-system
Copy link
Collaborator

Committed r294955 (251061@main): https://commits.webkit.org/251061@main

Reviewed commits have been landed. Closing PR #681 and removing active labels.

@webkit-early-warning-system webkit-early-warning-system removed the merge-queue Applied to send a pull request to merge-queue label May 27, 2022
@dcrousso dcrousso deleted the eng/239909 branch May 27, 2022 22:39
@dcrousso
Copy link
Member Author

the above revision/link is wrong

it's actually r294959 https://commits.webkit.org/251062@main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
HTML Editing For bugs in HTML editing support (including designMode and contentEditable).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants