Rhymond/go-money

Integer overflow in the Allocate

Opened this issue · 2 comments

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.

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

Yes I can have a look! 👍