Merge pull request #423 from cosmos/context-GetOp-never-crashes

Context get op never crashes
This commit is contained in:
Ethan Buchman 2018-02-06 14:51:49 -05:00 committed by GitHub
commit ed592cd94a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View File

@ -186,7 +186,7 @@ func (c Context) WithTxBytes(txBytes []byte) Context {
//----------------------------------------
// thePast
// Returns false if ver > 0.
// Returns false if ver <= 0 || ver > len(c.pst.ops).
// The first operation is version 1.
func (c Context) GetOp(ver int64) (Op, bool) {
return c.pst.getOp(ver)
@ -232,13 +232,13 @@ func (pst *thePast) version() int {
return pst.ver
}
// Returns false if ver > 0.
// Returns false if ver <= 0 || ver > len(pst.ops).
// The first operation is version 1.
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

20
types/context_test.go Normal file
View File

@ -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)
}
}