Allocate new(big.Int) in Copy method to deeply clone it (#278)

This commit is contained in:
Vadim Kulagin 2023-12-27 17:53:43 +03:00 committed by GitHub
parent f55dd56454
commit 6926256b71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 1 deletions

View file

@ -418,7 +418,7 @@ func NewFromFloatWithExponent(value float64, exp int32) Decimal {
func (d Decimal) Copy() Decimal {
d.ensureInitialized()
return Decimal{
value: &(*d.value),
value: new(big.Int).Set(d.value),
exp: d.exp,
}
}

View file

@ -560,6 +560,10 @@ func TestCopy(t *testing.T) {
origin := New(1, 0)
cpy := origin.Copy()
if origin.value == cpy.value {
t.Error("expecting copy and origin to have different value pointers")
}
if cpy.Cmp(origin) != 0 {
t.Error("expecting copy and origin to be equals, but they are not")
}