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

SR-13015: Decimal calculation incorrect on Linux #2827

Merged
merged 1 commit into from Jun 21, 2020

Conversation

finagolfin
Copy link
Contributor

@finagolfin finagolfin commented Jun 19, 2020

Fix typo and logic error in the overflow check in integerMultiply(), which was screwing up Decimal calculations under certain conditions.

I was able to reproduce on Android and spent hours tracking this down on there. I'm guessing it only worked on macOS because this code is unused on there.

@spevans
Copy link
Collaborator

spevans commented Jun 19, 2020

@swift-ci test

@finagolfin
Copy link
Contributor Author

Updated with correct overflow check.

@@ -1025,7 +1025,7 @@ fileprivate func integerMultiply<T:VariableLengthNumber>(_ big: inout T,
accumulator = UInt32(carry) + bigij + UInt32(right[j]) * UInt32(left[i])
carry = UInt16(truncatingIfNeeded:accumulator >> 16)
big[i+j] = UInt16(truncatingIfNeeded:accumulator)
} else if carry != 0 || (right[j] == 0 && left[j] == 0) {
} else if carry != 0 || (right[j] > 0 && left[i] > 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will overflow if the accumulator above would be non-zero, ie if there's a carry or both right and left elements that are multiplied are non-zero.

@spevans
Copy link
Collaborator

spevans commented Jun 20, 2020

Could you add in the extra tests in https://gist.githubusercontent.com/spevans/a5c9da89e91538abc688c5498fe4ed28/raw/e23ca9cdc21b87e2bcc1880ddafc5e01624bd9ce/sr13015.diff and squash it into one commit for easier back porting

@spevans
Copy link
Collaborator

spevans commented Jun 20, 2020

@swift-ci test linux

@finagolfin
Copy link
Contributor Author

Sure, will do.

Fix typo and logic error in the overflow check in integerMultiply(), which was
screwing up Decimal calculations under certain conditions.
@finagolfin
Copy link
Contributor Author

Rather than checking the same Decimal normalization in three different places, I removed my test and added your more extended tests instead.

@spevans
Copy link
Collaborator

spevans commented Jun 20, 2020

@swift-ci test linux

@spevans
Copy link
Collaborator

spevans commented Jun 20, 2020

@swift-ci test

@spevans spevans merged commit 6a1be26 into apple:master Jun 21, 2020
@finagolfin finagolfin deleted the decimal branch June 21, 2020 12:56
@finagolfin
Copy link
Contributor Author

You mentioned backporting this, should I submit a pull for the 5.3 branch? I presume 5.2 releases are done, or will that take this too?

@spevans
Copy link
Collaborator

spevans commented Jun 21, 2020

Lets get a backport for 5.3 going, Im not sure if it will make it into the release or will end up in 5.3.1 but having it ready to go will be useful, thanks.

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.

None yet

2 participants