From d48a3f82a4e965655945eaddaf7e9ccea7e35d5d Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 10 Aug 2023 16:10:38 -0400 Subject: [PATCH] fix(math): defend NewIntFromBigInt argument mutation (#17352) (cherry picked from commit 658a88a30991d05ae6386f21834a4058584ab863) # Conflicts: # math/CHANGELOG.md --- math/CHANGELOG.md | 73 +++++++++++++++++++++++++++++++++++++++++++++++ math/int.go | 4 ++- math/int_test.go | 13 +++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index 3b7c8e741ff7..ade1fd357aec 100644 --- a/math/CHANGELOG.md +++ b/math/CHANGELOG.md @@ -31,5 +31,78 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +<<<<<<< HEAD * [#13381](https://github.com/cosmos/cosmos-sdk/pull/13381) Add uint `IsNil` method. * [#12634](https://github.com/cosmos/cosmos-sdk/pull/12634) Move `sdk.Dec` to math package, call it `LegacyDec`. +======= +## Improvements + +* [#17109](https://github.com/cosmos/cosmos-sdk/pull/17109) Add `.ToLegacyDec()` method on `math.Int` type for converting to `math.LegacyDec`. +* [#16263](https://github.com/cosmos/cosmos-sdk/pull/16263) Improved `math/Int.Size` by computing the decimal digits count instead of firstly invoking .Marshal() then checking the length + +### Bug Fixes + +* [#17352](https://github.com/cosmos/cosmos-sdk/pull/17352) Ensure that modifying the argument to `NewIntFromBigInt` doesn't mutate the returned value. +* [#16266](https://github.com/cosmos/cosmos-sdk/pull/16266) fix: legacy dec power mut zero exponent precision. + +## [math/v1.0.1](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.0.1) - 2023-05-15 + +### Improvements + +* [#15768](https://github.com/cosmos/cosmos-sdk/pull/15768) Removed the second call to the `init` method for the global variable `grand`. +* [#16141](https://github.com/cosmos/cosmos-sdk/pull/16141) Speedup `LegacyDec.ApproxRoot` and `LegacyDec.ApproxSqrt`. + +### Bug Fixes + +* [#15714](https://github.com/cosmos/cosmos-sdk/pull/15714) `FormatInt` returns an error on empty string. + +## [math/v1.0.0](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.0.0) - 2023-03-23 + +### Bug Fixes + +* [#15506](https://github.com/cosmos/cosmos-sdk/issues/16605) Dec marshal shouldn't have side effects + +## [math/v1.0.0-rc.0](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.0.0-rc.0) - 2023-03-13 + +### Features + +* [#15043](https://github.com/cosmos/cosmos-sdk/issues/15043) add rand funcs to math + +### Bug Fixes + +* [#14922](https://github.com/cosmos/cosmos-sdk/issues/14922) check for negative precision + +### Testing + +* [#15215](https://github.com/cosmos/cosmos-sdk/issues/15215) fix `FormatDec` test + +## [math/v1.0.0-beta.6](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.0.0-beta.6) - 2023-02-06 + +### Features + +* [#14760](https://github.com/cosmos/cosmos-sdk/issues/14760) add collections key encoders and value encoders for common types. +* [#14166](https://github.com/cosmos/cosmos-sdk/issues/14166) math: add generics versions of Max, Min to cater to all numeric types +* [#13381](https://github.com/cosmos/cosmos-sdk/issues/13381) add uint `IsNil` method + +### Improvements + +* [#14010](https://github.com/cosmos/cosmos-sdk/issues/14010) math: optimize and test FormatInt + simplify LegacyNewDecFromStr +* [#12794](https://github.com/cosmos/cosmos-sdk/issues/12794) math: precompute & use square of precisionReuse instead of 2 repeated computations + +### Bug Fixes + +* [#14691](https://github.com/cosmos/cosmos-sdk/issues/14691) do not flatten events attributes by event types +* [#14252](https://github.com/cosmos/cosmos-sdk/issues/14252) math: add LegacyNewDecFromStr fuzzers + remove unnecessary error wrapping + +### Testing + +* [#14576](https://github.com/cosmos/cosmos-sdk/issues/14576) Added test cases for precisionMultiplier + +## [math/v1.0.0-beta.3](https://github.com/cosmos/cosmos-sdk/releases/tag/math/v1.0.0-beta.3) - 2022-07-20 + +### Bug Fixes + +* [#11996](https://github.com/cosmos/cosmos-sdk/issues/11996) math: fix Uint.Unmarshal's lack of negative value checking + + +>>>>>>> 658a88a30 (fix(math): defend NewIntFromBigInt argument mutation (#17352)) diff --git a/math/int.go b/math/int.go index 6811893f7a47..542f3320266d 100644 --- a/math/int.go +++ b/math/int.go @@ -104,6 +104,7 @@ func NewIntFromUint64(n uint64) Int { // NewIntFromBigInt constructs Int from big.Int. If the provided big.Int is nil, // it returns an empty instance. This function panics if the bit length is > 256. +// Note, the caller can safely mutate the argument after this function returns. func NewIntFromBigInt(i *big.Int) Int { if i == nil { return Int{} @@ -112,7 +113,8 @@ func NewIntFromBigInt(i *big.Int) Int { if i.BitLen() > MaxBitLen { panic("NewIntFromBigInt() out of bound") } - return Int{i} + + return Int{new(big.Int).Set(i)} } // NewIntFromString constructs Int from string diff --git a/math/int_test.go b/math/int_test.go index 5af450cb6c19..880eff2c0fc0 100644 --- a/math/int_test.go +++ b/math/int_test.go @@ -43,6 +43,19 @@ func (s *intTestSuite) TestFromUint64() { } } +func (s *intTestSuite) TestNewIntFromBigInt() { + i := math.NewIntFromBigInt(nil) + s.Require().True(i.IsNil()) + + r := big.NewInt(42) + i = math.NewIntFromBigInt(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 (s *intTestSuite) TestIntPanic() { // Max Int = 2^256-1 = 1.1579209e+77 // Min Int = -(2^256-1) = -1.1579209e+77