diff --git a/types/context.go b/types/context.go index a0f2c29b0..649f15b93 100644 --- a/types/context.go +++ b/types/context.go @@ -238,7 +238,7 @@ func (pst *thePast) getOp(ver int64) (Op, bool) { pst.mtx.RLock() defer pst.mtx.RUnlock() l := int64(len(pst.ops)) - if l < ver { + if l < ver || ver <= 0 { return Op{}, false } else { return pst.ops[ver-1], true diff --git a/types/context_test.go b/types/context_test.go new file mode 100644 index 000000000..36d8099b9 --- /dev/null +++ b/types/context_test.go @@ -0,0 +1,20 @@ +package types_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/types" + abci "github.com/tendermint/abci/types" +) + +func TestContextGetOpShouldNeverPanic(t *testing.T) { + var ms types.MultiStore + ctx := types.NewContext(ms, abci.Header{}, false, nil) + indices := []int64{ + -10, 1, 0, 10, 20, + } + + for _, index := range indices { + _, _ = ctx.GetOp(index) + } +}