-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix TextBox IME replacement range crash #5346
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3636,14 +3636,19 @@ final class TextBoxInputTextView: NSTextView { | |||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| override func insertText(_ insertString: Any, replacementRange: NSRange) { | ||||||||||||||||||||||||||||||||||
| let replacementRange = sanitizedTextStorageReplacementRange(replacementRange) | ||||||||||||||||||||||||||||||||||
| queueAutomaticAttachmentFileCleanup(in: replacementRange) | ||||||||||||||||||||||||||||||||||
| super.insertText(insertString, replacementRange: replacementRange) | ||||||||||||||||||||||||||||||||||
| flushAutomaticAttachmentFileCleanup() | ||||||||||||||||||||||||||||||||||
| onMarkedTextStateChanged(hasMarkedText()) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| override func setMarkedText(_ string: Any, selectedRange: NSRange, replacementRange: NSRange) { | ||||||||||||||||||||||||||||||||||
| super.setMarkedText(string, selectedRange: selectedRange, replacementRange: replacementRange) | ||||||||||||||||||||||||||||||||||
| super.setMarkedText( | ||||||||||||||||||||||||||||||||||
| string, | ||||||||||||||||||||||||||||||||||
| selectedRange: Self.sanitizedRange(selectedRange, upperBound: Self.textInputStringLength(string)), | ||||||||||||||||||||||||||||||||||
| replacementRange: sanitizedTextStorageReplacementRange(replacementRange) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+3648
to
+3651
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalize Line 3649 still lets Suggested fix- super.setMarkedText(
- string,
- selectedRange: Self.sanitizedRange(selectedRange, upperBound: Self.textInputStringLength(string)),
- replacementRange: sanitizedTextStorageReplacementRange(replacementRange)
- )
+ super.setMarkedText(
+ string,
+ selectedRange: Self.sanitizedMarkedSelectionRange(
+ selectedRange,
+ upperBound: Self.textInputStringLength(string)
+ ),
+ replacementRange: sanitizedTextStorageReplacementRange(replacementRange)
+ )
onMarkedTextStateChanged(hasMarkedText())
}
@@
- private static func sanitizedRange(_ range: NSRange, upperBound: Int) -> NSRange {
- guard range.location != NSNotFound else { return range }
+ private static func sanitizedMarkedSelectionRange(_ range: NSRange, upperBound: Int) -> NSRange {
let upperBound = max(0, upperBound)
+ guard range.location != NSNotFound else {
+ return upperBound > 0
+ ? NSRange(location: upperBound, length: 0)
+ : NSRange(location: NSNotFound, length: 0)
+ }
let location = min(max(0, range.location), upperBound)
let length = min(max(0, range.length), upperBound - location)
return NSRange(location: location, length: length)
}Also applies to: 3670-3675 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| onMarkedTextStateChanged(hasMarkedText()) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -3657,6 +3662,27 @@ final class TextBoxInputTextView: NSTextView { | |||||||||||||||||||||||||||||||||
| flushAutomaticAttachmentFileCleanup() | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| private func sanitizedTextStorageReplacementRange(_ range: NSRange) -> NSRange { | ||||||||||||||||||||||||||||||||||
| guard range.location != NSNotFound else { return range } | ||||||||||||||||||||||||||||||||||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||
| return Self.sanitizedRange(range, upperBound: attributedString().length) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| private static func sanitizedRange(_ range: NSRange, upperBound: Int) -> NSRange { | ||||||||||||||||||||||||||||||||||
| guard range.location != NSNotFound else { return range } | ||||||||||||||||||||||||||||||||||
| let upperBound = max(0, upperBound) | ||||||||||||||||||||||||||||||||||
| let location = min(max(0, range.location), upperBound) | ||||||||||||||||||||||||||||||||||
| let length = min(max(0, range.length), upperBound - location) | ||||||||||||||||||||||||||||||||||
| return NSRange(location: location, length: length) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| private static func textInputStringLength(_ string: Any) -> Int { | ||||||||||||||||||||||||||||||||||
| if let attributed = string as? NSAttributedString { | ||||||||||||||||||||||||||||||||||
| return attributed.length | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| let plain = (string as? String) ?? String(describing: string) | ||||||||||||||||||||||||||||||||||
| return (plain as NSString).length | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+3687
to
+3693
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| override func copy(_ sender: Any?) { | ||||||||||||||||||||||||||||||||||
| if copySelectedAttachments(to: .general) { | ||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
selectedRangeinsetMarkedTextis not covered by a regression testselectedRangehere is sanitized against the incoming marked-text string length (textInputStringLength(string)), which is the correct upper bound per theNSTextInputClientspec (the selection lives within the new marked string, not the text storage). However, there is no test that drives a stale or out-of-boundsselectedRangethroughsetMarkedTextto prove this branch works. The existing test only exercisesinsertText. A staleselectedRangefrom a multi-step IME composition sequence (e.g. selecting a different candidate after the replacement range was already consumed) would silently fall into this path and a future refactor could break it without a test to catch it.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!