From 53d90502f4d9c59bfabc5dffe3042cf1d8069644 Mon Sep 17 00:00:00 2001 From: Facundo Date: Mon, 23 Oct 2023 14:40:27 +0200 Subject: [PATCH 1/2] fix(math): NewUintFromBigInt argument mutation --- math/uint.go | 3 ++- math/uint_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/math/uint.go b/math/uint.go index 821ead066a2a..a33995009997 100644 --- a/math/uint.go +++ b/math/uint.go @@ -235,7 +235,8 @@ func checkNewUint(i *big.Int) (Uint, error) { if err := UintOverflow(i); err != nil { return Uint{}, err } - return Uint{i}, nil + + return Uint{new(big.Int).Set(i)}, nil } // RelativePow raises x to the power of n, where x (and the result, z) are scaled by factor b diff --git a/math/uint_test.go b/math/uint_test.go index 7d4a5a731fad..1cfb123d1d16 100644 --- a/math/uint_test.go +++ b/math/uint_test.go @@ -261,6 +261,16 @@ func (s *uintTestSuite) TestParseUint() { } } +func (s *uintTestSuite) TestNewUintFromBigInt() { + r := big.NewInt(42) + i := sdkmath.NewUintFromBigInt(r) + s.Require().Equal(r, i.BigInt()) + + // modify r and ensure i doesn't change + r = r.SetInt64(100) + s.Require().NotEqual(r, i.BigInt()) +} + func randuint() sdkmath.Uint { return sdkmath.NewUint(rand.Uint64()) } From 4efd5e90f6c858a1e651f404e1eaec7d503cfca1 Mon Sep 17 00:00:00 2001 From: Facundo Date: Mon, 23 Oct 2023 14:42:45 +0200 Subject: [PATCH 2/2] cl++ --- math/CHANGELOG.md | 1 + math/uint.go | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index 072fa738e0ad..19c6be5cbbf2 100644 --- a/math/CHANGELOG.md +++ b/math/CHANGELOG.md @@ -42,6 +42,7 @@ Ref: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.j ### Bug Fixes +* [#18214](https://github.com/cosmos/cosmos-sdk/pull/18214) Ensure that modifying the argument to `NewUIntFromBigInt` doesn't mutate the returned value. * [#17725](https://github.com/cosmos/cosmos-sdk/pull/17725) Fix state break in ApproxRoot. This has been present since math/v1.0.1. It changed the rounding behavior at precision end in an intermediary division from banker's to truncation. The truncation occurs from binary right shift in the case of square roots. The change is now reverted back to banker's rounding universally for any root. ## [math/v1.1.2](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.1.2) - 2023-08-21 diff --git a/math/uint.go b/math/uint.go index a33995009997..6542dd8f20ea 100644 --- a/math/uint.go +++ b/math/uint.go @@ -235,7 +235,6 @@ func checkNewUint(i *big.Int) (Uint, error) { if err := UintOverflow(i); err != nil { return Uint{}, err } - return Uint{new(big.Int).Set(i)}, nil }