Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nil pointer deref in distribution tax/rewward validation helpers #5620

Merged
merged 7 commits into from
Feb 7, 2020
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ of `types.CommitID`. During `Commit` of the root multi-store, `lastCommitInfo` i
and is only flushed to disk if it is a snapshot version. During `Query` of the root multi-store, if the request height
is the latest height, we'll use the store's `lastCommitInfo`. Otherwise, we fetch `commitInfo` from disk.
* (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.
* (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.
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved

### 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
34 changes: 34 additions & 0 deletions x/distribution/types/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
tt = tt
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.wantErr, validateCommunityTax(tt.args.i) != nil)
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, tt.wantErr, validateBaseProposerReward(tt.args.i) != nil)
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, tt.wantErr, validateBonusProposerReward(tt.args.i) != nil)
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
})
}
}