Crash in WPF controls on some languages
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
### Description
Our WPF application crashes on machines with a non-common local (EN-be). We have pinpointed this issue to https://github.com/dotnet/wpf/blob/22c053cf86737384a222d37c79e211639ff20f91/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputLanguageSource.cs#L216
The keyboard layout contains a high bit (1080C on a EN-be layout) than it is converted to a short which contains a negative value. When the controls ask for the CurrentInputLanguage in https://github.com/dotnet/wpf/blob/22c053cf86737384a222d37c79e211639ff20f91/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputLanguageSource.cs#L104 a new culture is create with this nevative value which crash the application.
One of the call stacks in our application
```
Description.....: culture ('-4066') must be a non-negative value. (Parameter 'culture')
Actual value was -4066.
Type............: System.ArgumentOutOfRangeException
HResult.........: -2146233086 (0x80131502)
Source..........: System.Private.CoreLib
TargetSite......: Void ThrowNegative[T](T, System.String)
DeclaringType...: System.ArgumentOutOfRangeException, System.Private.CoreLib
ProductVersion..: 9.0.17+f2c8152eed158e72950025393fde498c90a57a6b
================================================================
at System.ArgumentOutOfRangeException.ThrowNegative[T](T value, String paramName)
at System.Globalization.CultureInfo..ctor(Int32 culture, Boolean useUserOverride)
at System.Windows.Input.InputLanguageSource.get_CurrentInputLanguage()
at System.Windows.Documents.TextSelection.EnsureCaret(Boolean isBlinkEnabled, Boolean isSelectionActive, CaretScrollMethod scrollMethod)
at System.Windows.Documents.TextSelection.System.Windows.Documents.ITextSelection.UpdateCaretAndHighlight()
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.KeyboardDevice.ChangeFocus(DependencyObject focus, Int32 timestamp)
at System.Windows.Input.KeyboardDevice.Focus(DependencyObject focus, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
at System.Windows.Input.KeyboardDevice.Focus(IInputElement element)
at System.Windows.UIElement.Focus()
at Afas.Profit.Controls.Views.ViewFilterExtensions.SetFocusToFilterCell(GridControl source, String withValue, Boolean selectAll) in C:\Dev\profit-release\10\src\Anta\Projects\Cool
```
### Reproduction Steps
use powershell on an machine with the language `EN-be`.
```
Add-Type -Namespace Win -Name Kbd -MemberDefinition @'
[DllImport("user32.dll")] public static extern int GetKeyboardLayoutList(int nBuff, [Out] IntPtr[] lpList);
'@
$count = [Win.Kbd]::GetKeyboardLayoutList(0, $null)
$list = New-Object IntPtr[] $count
[void][Win.Kbd]::GetKeyboardLayoutList($count, $list)
foreach ($hkl in $list) {
$v = $hkl.ToInt64() -band 0xFFFFFFFF
$lw = $v -band 0xFFFF
$signed = if ($lw -ge 0x8000) { $lw - 0x10000 } else { $lw }
'{0:X8} langid=0x{1:X4} WPF passes {2}' -f $v, $lw, $signed
}
```
### Expected behavior
This should just work.
### Actual behavior
Application crash.
### Regression?
WinForms used to haver the same issue, but it was fixed in https://github.com/dotnet/winforms/pull/8573
### Known Workarounds
None
### Impact
_No response_
### Configuration
I am running .net 10 on a window 25200.9445 on a x64.
The issue only appears on machine with a languageid which have lager than 32,767 (decimal value).
### Other information
_No response_
Contributor guide
Research direction
Start in src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputLanguageSource.cs at the conversions around lines 104 and 216, then run the supplied PowerShell reproduction on an EN-be machine. Verify that CurrentInputLanguage handles keyboard language IDs above 32,767 without creating a negative culture or crashing WPF controls.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100