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

Integer overflow in the Allocate #136

Open
roelofjan-elsinga opened this issue Aug 14, 2023 · 2 comments · May be fixed by #147
Open

Integer overflow in the Allocate #136

roelofjan-elsinga opened this issue Aug 14, 2023 · 2 comments · May be fixed by #147

Comments

@roelofjan-elsinga
Copy link

Hello, we've got an integer overflow with the following code: https://go.dev/play/p/FjI8rCyWG3J

Code here as well:

package main

import "github.com/Rhymond/go-money"

func main() {

	ints := []int{
		7999800,
		133980000,
		156200000,
		1935580000,
		2041080000,
		287991,
		1729180000,
	}

	m := money.New(6004307791, "EUR")

	shares, err := m.Allocate(ints...)

	_ = err
	_ = shares

}

The error I'm seeing is this: panic: runtime error: index out of range [7] with length 7

This is because the total variable inside the Allocate gets an integer overflow and starts to go into the negative values, so the "leftover amounts" never get to zero.

@Rhymond
Copy link
Owner

Rhymond commented Aug 14, 2023

Hi @roelofjan-elsinga, great find! Would you be able to work on a fix?

@roelofjan-elsinga
Copy link
Author

Yes I can have a look! 👍

@pendolf pendolf linked a pull request May 25, 2024 that will close this issue
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 a pull request may close this issue.

2 participants