Skip to content

Commit

Permalink
Fix nil pointer dereference in x/distribution
Browse files Browse the repository at this point in the history
Before checking Dec's value, one has to ensure the internal big.Int
pointer is not nil first.

Closes: #5621
  • Loading branch information
Alessio Treglia committed Feb 7, 2020
1 parent b0c6c75 commit 4d69262
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ balances or a single balance by denom when the `denom` query parameter is presen

* (x/bank) [\#5531](https://github.com/cosmos/cosmos-sdk/issues/5531) Added missing amount event to MsgMultiSend, emitted for each output.
* (client) [\#5618](https://github.com/cosmos/cosmos-sdk/pull/5618) Fix crash on the client when the verifier is not set.
* (x/distribution) [\#5620](https://github.com/cosmos/cosmos-sdk/pull/5620) Fix nil pointer deref in distribution tax/rewward validation helpers.

### State Machine Breaking

Expand Down
9 changes: 9 additions & 0 deletions x/distribution/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func validateCommunityTax(i interface{}) error {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v.IsNil() {
return fmt.Errorf("community tax must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("community tax must be positive: %s", v)
}
Expand All @@ -108,6 +111,9 @@ func validateBaseProposerReward(i interface{}) error {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v.IsNil() {
return fmt.Errorf("base proposer reward must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("base proposer reward must be positive: %s", v)
}
Expand All @@ -124,6 +130,9 @@ func validateBonusProposerReward(i interface{}) error {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v.IsNil() {
return fmt.Errorf("bonus proposer reward must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("bonus proposer reward must be positive: %s", v)
}
Expand Down
33 changes: 33 additions & 0 deletions x/distribution/types/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package types

import (
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func Test_validateAuxFuncs(t *testing.T) {
type args struct {
i interface{}
}
tests := []struct {
name string
args args
wantErr bool
}{
{"wrong type", args{10.5}, true},
{"nil Int pointer", args{sdk.Dec{}}, true},
{"negative", args{sdk.NewDec(-1)}, true},
{"one dec", args{sdk.NewDec(1)}, false},
{"two dec", args{sdk.NewDec(2)}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.wantErr, validateCommunityTax(tt.args.i) != nil)
require.Equal(t, tt.wantErr, validateBaseProposerReward(tt.args.i) != nil)
require.Equal(t, tt.wantErr, validateBonusProposerReward(tt.args.i) != nil)
})
}
}

0 comments on commit 4d69262

Please sign in to comment.