Skip to content

Commit

Permalink
Allocate new(big.Int) in Copy method to deeply clone it (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimKulagin authored Dec 27, 2023
1 parent f55dd56 commit 6926256
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
4 changes: 4 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 6926256

Please sign in to comment.