Skip to content

Conversation

@flutteractionsbot
Copy link

@flutteractionsbot flutteractionsbot commented Oct 22, 2025

This pull request is created by automatic cherry pick workflow
Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request.

Issue Link:

What is the link to the issue this cherry-pick is addressing?

There does not appear to be a dedicated issue. The issue description can be found in the PR description:
#176329

The PR fixes a macOS text input crash caused by down-casting the string argument from Any to a NSString, from this API. The documentation says it can either be a NSString or NSAttributedString

Changelog Description:

Explain this cherry pick in one line that is accessible to most Flutter developers. See best practices for examples

Fixes a crash on macOS when the input method (or other services) inserts a NSAttributedString instead of NSString into a text field.

Impact Description:

What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping production app (the app crashes on launch)

App crashes on macOS when interacting with certain IMEs or other system services, if they decided to use NSAttributedString instead of NSString. It affects production apps.

Workaround:

Is there a workaround for this issue?

None. Users or app developers have little control over what the IME / services decide to do. The problematic code path in the engine directly interacts with system services so app developer can't workaround that.

Risk:

What is the risk level of this cherry-pick?

  • Low
  • Medium
  • High

Test Coverage:

Are you confident that your fix is well-tested by automated tests?

  • Yes
  • No

Validation Steps:

What are the steps to validate that this fix works?

The PR description does not have repro steps. @p1318k could you add the steps to trigger the bug here?

…lutter#176329)

## Description

This PR fixes a crash in the macOS text input plugin that occurs when
`NSAttributedString` is passed to the `insertText:replacementRange:`
method instead of `NSString`.

### Root Cause

The `insertText:replacementRange:` method of `NSTextInputClient`
protocol can receive either `NSString` or `NSAttributedString`. The
previous implementation directly called `UTF8String` on the input
parameter without type checking. Since `NSAttributedString` doesn't
respond to the `UTF8String` selector, this caused a crash with the
error:

-[NSConcreteMutableAttributedString UTF8String]: unrecognized selector
sent to instance

### Changes Made
- Added type checking to determine if the input is `NSAttributedString`
- Extract the underlying `NSString` using `[string string]` before
calling `UTF8String`
- This follows the same pattern already implemented in the
`setMarkedText:selectedRange:replacementRange:` method in the same file

### Crash Stack Trace

Thread 0 Crashed: 0 CoreFoundation __exceptionPreprocess 1
libobjc.A.dylib objc_exception_throw 2 CoreFoundation
-[NSObject(NSObject) doesNotRecognizeSelector:] 3 FlutterMacOS
-[FlutterTextInputPlugin insertText:replacementRange:]
(FlutterTextInputPlugin.mm:784) 4 AppKit
-[NSTextInputContext(NSInputContext_WithCompletion)
insertText:replacementRange:completionHandler:]

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

---------

Co-authored-by: Kim Ki Cheol <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@flutteractionsbot flutteractionsbot added the cp: review Cherry-picks in the review queue label Oct 22, 2025
@flutteractionsbot
Copy link
Author

@LongCatIsLooong please fill out the PR description above, afterwards the release team will review this request.

@flutter-dashboard
Copy link

This pull request was opened from and to a release candidate branch. This should only be done as part of the official Flutter release process. If you are attempting to make a regular contribution to the Flutter project, please close this PR and follow the instructions at Tree Hygiene for detailed instructions on contributing to Flutter.

Reviewers: Use caution before merging pull requests to release branches. Ensure the proper procedure has been followed.

@github-actions github-actions bot added engine flutter/engine related. See also e: labels. a: desktop Running on desktop platform-macos labels Oct 22, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses a crash on macOS within insertText: when an NSAttributedString is provided. The fix correctly handles this by checking the input type and extracting the plain string if it's an NSAttributedString. The change is accompanied by two new unit tests that validate the fix for both regular and empty attributed strings. The implementation is sound, though I have one suggestion to make it more robust against unexpected input types.

@okorohelijah okorohelijah changed the title [CP-beta][CP-stable] Fix crash when NSAttributedString is passed to insertText on macOS [CP-beta] Fix crash when NSAttributedString is passed to insertText on macOS Oct 23, 2025
@camsim99 camsim99 added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 27, 2025
@auto-submit auto-submit bot merged commit d7ec7e8 into flutter:flutter-3.38-candidate.0 Oct 27, 2025
169 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Nov 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: desktop Running on desktop autosubmit Merge PR when tree becomes green via auto submit App cp: review Cherry-picks in the review queue engine flutter/engine related. See also e: labels. platform-macos

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants