Skip to content

Commit

Permalink
Merge pull request #1357 from CosmWasm/mergify/bp/releases/v0.3x/pr-1354
Browse files Browse the repository at this point in the history
More verbose error message (backport #1354)
  • Loading branch information
alpe authored Apr 21, 2023
2 parents 83b5be8 + d3de781 commit 0032f27
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
10 changes: 8 additions & 2 deletions x/wasm/ioutils/ioutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@ import (
"compress/gzip"
"io"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/CosmWasm/wasmd/x/wasm/types"
)

// Uncompress expects a valid gzip source to unpack or fails. See IsGzip
func Uncompress(gzipSrc []byte, limit uint64) ([]byte, error) {
if uint64(len(gzipSrc)) > limit {
return nil, types.ErrLimit
return nil, types.ErrLimit.Wrapf("max %d bytes", limit)
}
zr, err := gzip.NewReader(bytes.NewReader(gzipSrc))
if err != nil {
return nil, err
}
zr.Multistream(false)
defer zr.Close()
return io.ReadAll(LimitReader(zr, int64(limit)))
bz, err := io.ReadAll(LimitReader(zr, int64(limit)))
if types.ErrLimit.Is(err) {
return nil, sdkerrors.Wrapf(err, "max %d bytes", limit)
}
return bz, err
}

// LimitReader returns a Reader that reads from r
Expand Down
5 changes: 3 additions & 2 deletions x/wasm/ioutils/ioutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/rand"

"github.com/CosmWasm/wasmd/x/wasm/types"
)
Expand Down Expand Up @@ -52,8 +53,8 @@ func TestUncompress(t *testing.T) {
src: asGzip(bytes.Repeat([]byte{0x1}, maxSize+1)),
expError: types.ErrLimit,
},
"handle other big gzip output": {
src: asGzip(bytes.Repeat([]byte{0x1}, 2*maxSize)),
"handle big gzip archive": {
src: asGzip(rand.Bytes(2 * maxSize)),
expError: types.ErrLimit,
},
}
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (k Keeper) create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte,
ctx.GasMeter().ConsumeGas(k.gasRegister.UncompressCosts(len(wasmCode)), "Uncompress gzip bytecode")
wasmCode, err = ioutils.Uncompress(wasmCode, uint64(types.MaxWasmSize))
if err != nil {
return 0, checksum, sdkerrors.Wrap(types.ErrCreateFailed, err.Error())
return 0, checksum, types.ErrCreateFailed.Wrap(sdkerrors.Wrap(err, "uncompress wasm archive").Error())
}
}

Expand Down Expand Up @@ -191,7 +191,7 @@ func (k Keeper) importCode(ctx sdk.Context, codeID uint64, codeInfo types.CodeIn
var err error
wasmCode, err = ioutils.Uncompress(wasmCode, uint64(types.MaxWasmSize))
if err != nil {
return sdkerrors.Wrap(types.ErrCreateFailed, err.Error())
return types.ErrCreateFailed.Wrap(sdkerrors.Wrap(err, "uncompress wasm archive").Error())
}
}
newCodeHash, err := k.wasmVM.Create(wasmCode)
Expand Down

0 comments on commit 0032f27

Please sign in to comment.