-
Notifications
You must be signed in to change notification settings - Fork 100
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
Possible overflow exception when casting IntPtr
/UIntPtr
to nint
/nuint
#661
Comments
Which casts are problematic? If casting from
What type is being cast to Do any of these casts relate to converting between |
FWIW I tried this: IntPtr maxSigned = (IntPtr)long.MaxValue;
UIntPtr maxUnsigned = (UIntPtr)ulong.MaxValue;
UIntPtr signedToUnsigned = (UIntPtr)(long)maxSigned; // intermediate casts required to compile
IntPtr unsignedToSigned = (IntPtr)(ulong)maxUnsigned;
nint maxSigned2 = (nint)long.MaxValue; // compiler emits overflow warnings
nuint maxUnsigned2 = (nuint)ulong.MaxValue;
nuint signedToUnsigned2 = (nuint)maxSigned2;
nint unsignedToSigned2 = (nint)maxUnsigned2;
nint signedConverted = maxSigned;
nuint unsignedConverted = maxUnsigned; This didn't throw at runtime for net472 or net6. |
This depends on the value of CheckForOverflowUnderflow in your project file. I'm quite sure that both:
throw when CheckForOverflowUnderflow is true on 64-bit, and these:
also on 32-bit platforms. If you just want a (possibly truncated) bit-copy, then explicitly using
will never throw. |
Thanks, @dorssel. I don't think it's that simple though. The checked/unchecked switch in the project only impacts code in that particular compilation. The Casting |
Ah, yes. You are right. Same still for .NET 7 preview. |
More on this at dotnet/csharplang#4665 (comment). |
I'm going to close this issue as I don't see any issue to fix in CsWin32. If anyone can point to something specifically (ideally with example code that's broken) we can reactivate. |
Found this comment in Winforms, which might help sway us towards wanting to use
nint
/nuint
overIntPtr
/UIntPtr
.https://github.com/dotnet/winforms/blob/179df82381b5df175d043c2190306354f8b46cae/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Message.cs#L39-L50
The text was updated successfully, but these errors were encountered: