Skip to content

Commit

Permalink
fix decodeBytes() integer overflow on 32-bit systems
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Dec 12, 2020
1 parent f5c104d commit 95228fd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Bug Fixes

- Fix integer overflow in `decodeBytes()` that can cause out-of-memory errors on 32-bit machines for certain inputs.

## 0.15.0 (November 23, 2020)

The IAVL project has moved from https://github.com/tendermint/iavl to
Expand Down
5 changes: 5 additions & 0 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ func decodeBytes(bz []byte) ([]byte, int, error) {
if err != nil {
return nil, n, err
}
// check for overflows
if int(size) < 0 {
return nil, n, fmt.Errorf("invalid negative length %v decoding []byte", size)
}
// ^uint(0) >> 1 will help determine the max int value variably on 32-bit and 64-bit machines.
if uint64(n)+size >= uint64(^uint(0)>>1) {
return nil, n, fmt.Errorf("invalid out of range length %v decoding []byte", uint64(n)+size)
}
if len(bz) < n+int(size) {
return nil, n, fmt.Errorf("insufficient bytes decoding []byte of length %v", size)
}
Expand Down

0 comments on commit 95228fd

Please sign in to comment.