From 72c8a04558383dfe4bae383db0e3c33ced84fd26 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Fri, 7 Feb 2020 19:09:19 +0100 Subject: [PATCH] Merge PR #5620: Fix nil pointer deref in distribution tax/rewward validation helpers --- CHANGELOG.md | 1 + x/distribution/types/params.go | 9 ++++++++ x/distribution/types/params_test.go | 34 +++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 x/distribution/types/params_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 9af4693d4ef4..a2a5de776a4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (client) [\#5618](https://github.com/cosmos/cosmos-sdk/pull/5618) Fix crash on the client when the verifier is not set. * (crypto/keys/mintkey) [\#5823](https://github.com/cosmos/cosmos-sdk/pull/5823) fix errors handling in UnarmorPubKeyBytes (underlying armoring function's return error was not being checked). +* (x/distribution) [\#5620](https://github.com/cosmos/cosmos-sdk/pull/5620) Fix nil pointer deref in distribution tax/rewward validation helpers. ### Improvements diff --git a/x/distribution/types/params.go b/x/distribution/types/params.go index ac3387e9c48b..f4530dd81c3b 100644 --- a/x/distribution/types/params.go +++ b/x/distribution/types/params.go @@ -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) } @@ -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) } @@ -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) } diff --git a/x/distribution/types/params_test.go b/x/distribution/types/params_test.go new file mode 100644 index 000000000000..143e2d30d77d --- /dev/null +++ b/x/distribution/types/params_test.go @@ -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 { + tt = tt + 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) + }) + } +}