diff --git a/native/compressor.go b/native/compressor.go index 08c2cf3..8242563 100644 --- a/native/compressor.go +++ b/native/compressor.go @@ -60,10 +60,14 @@ func (c *Compressor) Compress(in, out []byte, f compress) (int, []byte, error) { if err == errorShortBuffer { // if still doesn't fit (shouldn't happen at all) out = make([]byte, 1000+len(in)*2) n, _, _ := c.compress(in, out, f) - return n, out[:n], errors.New("libdeflate: native: compressed data is much larger than uncompressed") + outSmallCap := make([]byte, len(out)) + copy(outSmallCap, out) + return n, outSmallCap, errors.New("libdeflate: native: compressed data is much larger than uncompressed") } - return n, out[:n], nil + outSmallCap := make([]byte, len(out)) + copy(outSmallCap, out) + return n, outSmallCap, nil } func (c *Compressor) compress(in, out []byte, f compress) (int, []byte, error) { diff --git a/v2/native/compressor.go b/v2/native/compressor.go index 572e9a9..f19e555 100644 --- a/v2/native/compressor.go +++ b/v2/native/compressor.go @@ -60,10 +60,14 @@ func (c *Compressor) Compress(in, out []byte, f compress) (int, []byte, error) { if err == errorShortBuffer { // if still doesn't fit (shouldn't happen at all) out = make([]byte, 1000+len(in)*2) n, _, _ := c.compress(in, out, f) - return n, out[:n], errors.New("libdeflate: native: compressed data is much larger than uncompressed") + outSmallCap := make([]byte, len(out)) + copy(outSmallCap, out) + return n, outSmallCap, errors.New("libdeflate: native: compressed data is much larger than uncompressed") } - return n, out[:n], nil + outSmallCap := make([]byte, len(out)) + copy(outSmallCap, out) + return n, outSmallCap, nil } func (c *Compressor) compress(in, out []byte, f compress) (int, []byte, error) {