Skip to content

Conversation

kerams
Copy link
Contributor

@kerams kerams commented Jul 22, 2025

What does the pull request do?

Fixing some more occurrences of crashes in iOS text input. Unfortunately, I don't know how to reproduce this issue - the exception was reported automatically from user devices.

What is the current behavior?

GetPosition may return null, which is in accordance with official documentation. However, if I understand how this all works, iOS might then feed us that null back via other methods in the UITextInput implementation, and since there are no input parameter validations anywhere, we're shooting ourselves in the foot.

What is the updated/expected behavior with this PR?

GetPosition always returns valid values, which I find more clean than validating parameters.

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0057751-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul MrJul added bug os-ios backport-candidate-11.3.x Consider this PR for backporting to 11.3 branch labels Jul 30, 2025
Copy link
Member

@MrJul MrJul left a comment

Choose a reason for hiding this comment

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

GetPosition always returns valid values, which I find more clean than validating parameters.

Unfortunately, the official documentation you mentioned states:

Return nil if the computed text position is less than 0 or greater than the length of the backing string.
So we must follow this rule rather than clamping the resulting value.

I would change GetPositionCore to take a nullable AvaloniaTextPosition? as the first parameter. If it's null, return null.

That should take care of the original issue.

@kerams
Copy link
Contributor Author

kerams commented Jul 30, 2025

No, it wouldn't, because there a dozen other methods that take UITextPosition and don't consider nullability at all (and to add insult to injury, the interface doesn't even specify UITextPosition parameters as nullable), so I'd just get a crash elsewhere. I don't see why we would have to return null - can iOS do anything with the returned AvaloniaTextPosition instances other than passing it back to us in other method calls?

I think parameter handling is definitely in need of a thorough refactoring, because #19315 is caused by incorrect usage of UITextPosition too, but the hotfix in this PR seems harmless.

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0058016-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-candidate-11.3.x Consider this PR for backporting to 11.3 branch bug os-ios
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants