From 6926256b719269f340681d6d59ddc44e1cb0356f Mon Sep 17 00:00:00 2001 From: Vadim Kulagin <10106384+VadimKulagin@users.noreply.github.com> Date: Wed, 27 Dec 2023 17:53:43 +0300 Subject: [PATCH] Allocate new(big.Int) in Copy method to deeply clone it (#278) --- decimal.go | 2 +- decimal_test.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/decimal.go b/decimal.go index c614ea7..5982f81 100644 --- a/decimal.go +++ b/decimal.go @@ -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, } } diff --git a/decimal_test.go b/decimal_test.go index 2b3a99e..c6d2b24 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -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") }