Skip to content

Commit

Permalink
s2: Expand destination slice if big enough. (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Jan 11, 2020
1 parent 5c1f19d commit da94045
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion s2/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func Decode(dst, src []byte) ([]byte, error) {
if err != nil {
return nil, err
}
if dLen <= len(dst) {
if dLen <= cap(dst) {
dst = dst[:dLen]
} else {
dst = make([]byte, dLen)
Expand Down
4 changes: 3 additions & 1 deletion s2/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ import (
func Encode(dst, src []byte) []byte {
if n := MaxEncodedLen(len(src)); n < 0 {
panic(ErrTooLarge)
} else if len(dst) < n {
} else if cap(dst) < n {
dst = make([]byte, n)
} else {
dst = dst[:n]
}

// The block starts with the varint-encoded length of the decompressed bytes.
Expand Down

0 comments on commit da94045

Please sign in to comment.