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

implement Coin.Multiply overflow check #351

Merged
merged 3 commits into from
Feb 25, 2019
Merged

Conversation

husio
Copy link
Contributor

@husio husio commented Feb 25, 2019

implement Coin.Multiply overflow check

resolve #326

@husio husio requested a review from ethanfrey February 25, 2019 10:46
@husio husio force-pushed the coins_overflow_issue_326 branch from 7649fa3 to 6e65a14 Compare February 25, 2019 11:40
@husio husio changed the title wip implement Coin.Multiply overflow check Feb 25, 2019
@husio husio marked this pull request as ready for review February 25, 2019 11:41
@husio husio requested a review from alpe as a code owner February 25, 2019 11:41
Copy link
Contributor

@ethanfrey ethanfrey left a comment

Choose a reason for hiding this comment

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

Added some comments.
Some got lost as diff changed.. Will re-add them

x/coin.go Outdated Show resolved Hide resolved
x/coin.go Outdated Show resolved Hide resolved
@@ -76,7 +76,11 @@ func (c Coin) Divide(pieces int64) (Coin, Coin, error) {
}

// Multiply returns the result of a coin value multiplication.
func (c Coin) Multiply(times int64) Coin {
func (c Coin) Multiply(times int64) (Coin, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If we enforce int32, I think we guarantee to have no overflows ever....
But we don't have to do so

x/coin_test.go Show resolved Hide resolved
x/coin.go Outdated
return 0, true
}
c := a * b
if (c < 0) == ((a < 0) != (b < 0)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you only need the inner check, c/b == a
I was just pasting that answer I got from https://stackoverflow.com/a/1815371

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wow, this is a smart check! c := a * b therefore c / b == a must be true. I will upgrade.

x/coin.go Outdated

// Borrowed from
// https://github.com/JohnCGriffin/overflow/blob/master/overflow_impl.go#L336
func mul64(a, b int64) (int64, bool) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please state that the bool is true if valid, false if overflow

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will change the bool to error. It won't require a comment then 💪


// Normalize if fractional value overflows.
if frac > FracUnit {
whole += frac / FracUnit
if n := whole + frac/FracUnit; n < whole {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice check here... gotta have a clever test that multiply doesn't overflow until we carry the fractional, but good to cover this edge

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@codecov-io

This comment has been minimized.

Copy link
Contributor

@ethanfrey ethanfrey left a comment

Choose a reason for hiding this comment

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

Looks good

@husio husio merged commit 6d0f52d into master Feb 25, 2019
@husio husio deleted the coins_overflow_issue_326 branch February 25, 2019 13:38
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.

Check for overflow in Coin.Multiply and return error
3 participants