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

Fix issue with long.MaxValue + added tests #674

Closed
wants to merge 1 commit into from

Conversation

Falco20019
Copy link

Fixes #673 by using unchecked instead of checked cast. Also added test cases for avoiding regression.

@aaubry aaubry mentioned this pull request Apr 29, 2022
@EdwardCooke
Copy link
Collaborator

EdwardCooke commented Jul 12, 2022

The issue I see with this is if someone gets a number greater than long.MaxValue (9223372036854775807) it will no longer be correct.

Example:

Console.WriteLine(unchecked(-(long)9223372036854775809));

results in 9223372036854775807

As such, I dont think we want to do this.

@EdwardCooke
Copy link
Collaborator

One way to fix it would be to check if the number is the positive of the negative and if so, return long.minvalue.

            if (isNegative)
            {
                long toCast;

                // the only real difference between long.minvalue and maxvalue is 1.
                // since the number part of long.minvalue is larger than long.maxvalue
                // we will check to see if it is the number and then return the min value
                // otherwise we will run it through checked to validate it is within our range
                if (result == 9223372036854775808)
                {
                    toCast = long.MinValue;
                }
                else
                {
                    toCast = checked(-(long)result);
                }

                return CastInteger(toCast, typeCode);
            }
            else
            {
                return CastInteger(result, typeCode);
            }
        }

@EdwardCooke
Copy link
Collaborator

I've used parts of your tests but replaced the logic with what I posted above. Closing this in preference of #702.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deserialization of long.MinValue throws an OverflowException
2 participants