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

Casting negative double to uint does not seem to meet the specification, and works inconsistently across platforms #45429

Closed
antonfirsov opened this issue Dec 1, 2020 · 4 comments

Comments

@antonfirsov
Copy link
Member

If I'm not missing anything, the following code should throw System.OverflowException according to the Explicit numeric conversions section of the C# language specification:

double a = -1;
uint b = (uint)a;
Console.WriteLine(b);

Instead, it will print uint.MaxValue on x86.
On Arm64, it seems to return 0 based on my understanding of #45249. I don't have a local repro, but the problem seems to be with the following code on CI:

keepaliveinterval = (uint)_tcpKeepAliveInterval.TotalMilliseconds,
keepalivetime = (uint)_tcpKeepAliveTime.TotalMilliseconds

When we are casting Timeout.InfiniteTimeSpan.TotalMilliseconds == -1.0 to uint the conversion seems to result in 0 on Arm64, while (also incorreclty) returning -1 on x86.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Dec 1, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@antonfirsov antonfirsov added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI and removed area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI untriaged New issue has not been triaged by the area owner labels Dec 1, 2020
@antonfirsov
Copy link
Member Author

I forgot that default context is unchecked so it's fine, need more sleep.

@EgorBo
Copy link
Member

EgorBo commented Dec 1, 2020

If it's 0 on Arm64 then it's a bug 🤔

@antonfirsov
Copy link
Member Author

According to the C# spec the unchecked case goes as following:

  • If the value of the operand is NaN or infinite, the result of the conversion is an unspecified value of the destination type.
  • Otherwise, the source operand is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type then this value is the result of the conversion.
  • Otherwise, the result of the conversion is an unspecified value of the destination type.

I'm wondering how do we handle the "unspecified value" cases. Is it our equivalent of C++ UB?

@ghost ghost locked as resolved and limited conversation to collaborators Dec 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants