xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] UWP ArgumentOutOfRangeException occures when writing into Entry in password mode #12486

Open mbalous opened 4 years ago

mbalous commented 4 years ago

Description

Writing into Xamarin.Forms Entry on UWP with IsPassword set to true causes ArgumentOutOfRangeException

Steps to Reproduce

  1. Put an Entry into a page eg.: <Entry IsPassword="True"/>
  2. Repeatedly press a key on keyboard
  3. While still pressing random keys, select random parts of text with your mouse.

FormsTestBench.zip

Expected Behavior

No exception to occur.

Actual Behavior

Exception occurs.

Stacktrace:

at System.String.Insert(Int32 startIndex, String value)
at Xamarin.Forms.Platform.UWP.FormsTextBox.DetermineTextFromPassword(String realText, Int32 start, String passwordText)
at Xamarin.Forms.Platform.UWP.FormsTextBox.OnTextChanged(Object sender, TextChangedEventArgs textChangedEventArgs)

Basic Information

Reproduction Link

Run FormsTestBench.UWP and use the Entry provided FormsTestBench.zip

Workaround

Workaround for this issue could be similar to this: https://github.com/xamarin/Xamarin.Forms/issues/8644 https://github.com/xamarin/Xamarin.Forms/pull/8645

rachelkang commented 4 years ago

Hi @mbalous - thanks for submitting this issue! I wasn't able to reproduce the exception that you're seeing on UWP. Would you be able to share a screen recording and provide any other details of the occurrence, so that we can try to better understand what you're experiencing? Thanks :)

mbalous commented 4 years ago

Hello @rachelkang , sure thing. Here you go: https://www.youtube.com/watch?v=WEWC6AKKjhM

You can see the screen, keyboard and the mouse at the same time. At the end of the video I had to increase my typing and selecting speed for the bug to occur.

ghost commented 3 years ago

Hello, it happened to me too ... Do we know when it will be corrected?

mkrnic commented 2 years ago

An easier way to (unfortunately) reproduce it:

Enter some text in an IsPassword entry. Select all of that text, and then type a couple of characters quickly (pressing two keys at once might do the trick; or just mash the keyboard). It will crash, likely even on the first try; if not, repeat. It won't take long, a few tries at most.

Parameter name: count
   at System.String.Remove(Int32 startIndex, Int32 count)
   at Xamarin.Forms.Platform.UWP.FormsTextBox.OnKeyDown(KeyRoutedEventArgs e)

Having an intermediate control that does obfuscation, has internal race conditions and no error handling is not cool, Xamarin people. Not cool at all.

I "solved" it by doing this in UWP App.xaml.cs, and it doesn't seem that it has bad consequences. It might, I'm not sure. It's probably better than crashing the application entirely and making users call our tech support to complain that our app is unstable (which it is, due to many many bugs like this one).

        private async void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
        {
            var exceptionStackTrace = e.Exception.StackTrace;

            e.Handled = true;

            if (exceptionStackTrace.Contains("at Xamarin.Forms.Platform.UWP.FormsTextBox.OnKeyDown(KeyRoutedEventArgs"))
            {
                System.Diagnostics.Debug.WriteLine("FormsTextBox crash handled");
                return;
            }
            ...

BTW, if anyone's having problems with logging UWP crashes, make sure that var foo = e.Exception.StackTrace is the first line in your handler. Took me a long time to figure out.

Also note the "unfinished" string in the Contains method above. That's because I have production errors like this:

 System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
     at System.String.Remove(Int32, Int32) + 0x12e
     at Xamarin.Forms.Platform.UWP.FormsTextBox.OnKeyDown(KeyRoutedEventArgs) + 0x201
     at Windows.UI.Xaml.Automation.Peers.AutomationPeer.global::Windows.UI.Xaml.Automation.Peers.IAutomationPeerOverrides.IsEnabledCore() + 0x7
     at foo!<BaseAddress>+0x3e066a2

Which is just slightly different than the exception above ("(KeyRoutedEventArgs e)" vs. "(KeyRoutedEventArgs)" and might be a debug-release thing.