Skip to content
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

short.Parse throws FormatException #111276

Closed
Herdo opened this issue Jan 10, 2025 · 4 comments
Closed

short.Parse throws FormatException #111276

Herdo opened this issue Jan 10, 2025 · 4 comments
Labels
arch-wasm WebAssembly architecture area-System.Globalization os-browser Browser variant of arch-wasm

Comments

@Herdo
Copy link

Herdo commented Jan 10, 2025

Description

I have one of my users reporting an issue with our Blazor WASM app.
After investigating, we found the error being that "-1" cannot be parsed with short.Parse for him.
He's the only user with that issue from about 200 users.

The stacktrace:

blazor.web.js:1 
 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Format_InvalidStringWithValue, -1
System.FormatException: Format_InvalidStringWithValue, -1
   at System.Number.ThrowFormatException[Char](ReadOnlySpan`1 )
   at System.Number.ThrowOverflowOrFormatException[Char,Int16](ParsingStatus , ReadOnlySpan1 )
   at System.Number.ParseBinaryInteger[Char,Int16](ReadOnlySpan1 , NumberStyles , NumberFormatInfo )
   at System.Int16.Parse(ReadOnlySpan1 , NumberStyles , IFormatProvider )
   at System.Int16.Parse(String , NumberStyles , IFormatProvider )
   at System.Int16.Parse(String )
   at UNITS.Client.Components.EventDetails.RosterCards.<>c.<OnInitializedAsync>b__77_0(KeyValuePair2 m)
   at System.Linq.Enumerable.ToDictionary[KeyValuePair2,GroupNumber,Int32](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer)
   at System.Linq.Enumerable.ToDictionary[KeyValuePair2,GroupNumber,Int32](IEnumerable1 source, Func2 keySelector, Func`2 elementSelector)
   at UNITS.Client.Components.EventDetails.RosterCards.OnInitializedAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task , ComponentState )

Reproduction Steps

Not reliably reproduceable on other machines. Here are the steps anyway:

  1. Open Blazor WASM app in Microsoft Edge.
  2. Request payload via web API from server.
    • Response is {"-1":26}
  3. Deserialize response with NSwag generated client to a Dictionary<string, int>.
    • Deserialized dictionary is {{"-1", 26}}
  4. Convert deserialized dictionary to Dictionary<Id, int>:
var converted = response.ToDictionary(m => (Id)short.Parse(m.Key), m => m.Value);

Expected behavior

"-1" should be parsed correctly to the Int16 -1.

Actual behavior

System.FormatException is thrown.

Regression?

It still worked on 2025-01-03. It's not working any more since 2025-01-10.
The application is running the same version for both dates, the hosting Docker container wasn't even restarted.

Known Workarounds

For that one user, he can use another browser than Edge.
Edge works for other users.

Configuration

  • .NET 8.0
  • Windows 11
  • x64
  • French language version
  • Blazor, running in Edge 131.0.2903.112 (Official build) (64-bit)

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jan 10, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 10, 2025
@huoyaoyuan huoyaoyuan added area-System.Globalization and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jan 10, 2025
@4nonym0us
Copy link

4nonym0us commented Jan 10, 2025

The error means you are trying to parse a number using incorrect format.

Use overload, which accepts IFormatProvider if you need to specify the culture explicitly, otherwise Thread.CurrentThread.CurrentCulture is used as a fallback. Not specifying the culture leads to the FormatException for that user because your input doesn't match their culture preferences (it works in different browser for them because they have different language preferences there):

public static short Parse(string s, IFormatProvider? provider)
short.Parse("-1", CultureInfo.CreateSpecificCulture("ar-SA")); // FormatException
short.Parse("؜-1", CultureInfo.CreateSpecificCulture("ar-SA")); // -1

short.Parse("-1", CultureInfo.InvariantCulture); // -1
short.Parse("؜-1", CultureInfo.InvariantCulture); // FormatException

TLDR: Replace short.Parse(m.Key) with short.Parse(m.Key, CultureInfo.InvariantCulture) if your app supports only invariant culture or make sure that your Key is in correct format (if your app is designed to support multiple cultures).

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-globalization
See info in area-owners.md if you want to be subscribed.

@jkotas jkotas added arch-wasm WebAssembly architecture os-browser Browser variant of arch-wasm labels Jan 10, 2025
Copy link
Contributor

Tagging subscribers to 'arch-wasm': @lewing
See info in area-owners.md if you want to be subscribed.

@Herdo
Copy link
Author

Herdo commented Jan 10, 2025

The error means you are trying to parse a number using incorrect format.

Use overload, which accepts IFormatProvider if you need to specify the culture explicitly, otherwise Thread.CurrentThread.CurrentCulture is used as a fallback. Not specifying the culture leads to the FormatException for that user because your input doesn't match their culture preferences (it works in different browser for them because they have different language preferences there):

public static short Parse(string s, IFormatProvider? provider)

short.Parse("-1", CultureInfo.CreateSpecificCulture("ar-SA")); // FormatException
short.Parse("؜-1", CultureInfo.CreateSpecificCulture("ar-SA")); // -1

short.Parse("-1", CultureInfo.InvariantCulture); // -1
short.Parse("؜-1", CultureInfo.InvariantCulture); // FormatException

TLDR: Replace short.Parse(m.Key) with short.Parse(m.Key, CultureInfo.InvariantCulture) if your app supports only invariant culture or make sure that your Key is in correct format (if your app is designed to support multiple cultures).

Yeah, you're correct. The language is set to Hebrew. Didn't think about RTL issue. Thank you!

@Herdo Herdo closed this as completed Jan 10, 2025
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-wasm WebAssembly architecture area-System.Globalization os-browser Browser variant of arch-wasm
Projects
None yet
Development

No branches or pull requests

4 participants