Skip to content

Commit

Permalink
fix(math): defend NewIntFromBigInt argument mutation (#17352)
Browse files Browse the repository at this point in the history
(cherry picked from commit 658a88a)

# Conflicts:
#	math/CHANGELOG.md
  • Loading branch information
alexanderbez authored and mergify[bot] committed Oct 23, 2023
1 parent d003fa1 commit d48a3f8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
73 changes: 73 additions & 0 deletions math/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<!-- generated by git-cliff -->
>>>>>>> 658a88a30 (fix(math): defend NewIntFromBigInt argument mutation (#17352))
4 changes: 3 additions & 1 deletion math/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions math/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d48a3f8

Please sign in to comment.