Skip to content

Commit 25e4748

Browse files
authored
Merge pull request #22 from karelbilek/master
Return smaller slices when compressing
2 parents be494b5 + 44664fd commit 25e4748

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

native/compressor.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ func (c *Compressor) Compress(in, out []byte, f compress) (int, []byte, error) {
6060
if err == errorShortBuffer { // if still doesn't fit (shouldn't happen at all)
6161
out = make([]byte, 1000+len(in)*2)
6262
n, _, _ := c.compress(in, out, f)
63-
return n, out[:n], errors.New("libdeflate: native: compressed data is much larger than uncompressed")
63+
outSmallCap := make([]byte, len(out))
64+
copy(outSmallCap, out)
65+
return n, outSmallCap, errors.New("libdeflate: native: compressed data is much larger than uncompressed")
6466
}
6567

66-
return n, out[:n], nil
68+
outSmallCap := make([]byte, len(out))
69+
copy(outSmallCap, out)
70+
return n, outSmallCap, nil
6771
}
6872

6973
func (c *Compressor) compress(in, out []byte, f compress) (int, []byte, error) {

v2/native/compressor.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ func (c *Compressor) Compress(in, out []byte, f compress) (int, []byte, error) {
6060
if err == errorShortBuffer { // if still doesn't fit (shouldn't happen at all)
6161
out = make([]byte, 1000+len(in)*2)
6262
n, _, _ := c.compress(in, out, f)
63-
return n, out[:n], errors.New("libdeflate: native: compressed data is much larger than uncompressed")
63+
outSmallCap := make([]byte, len(out))
64+
copy(outSmallCap, out)
65+
return n, outSmallCap, errors.New("libdeflate: native: compressed data is much larger than uncompressed")
6466
}
6567

66-
return n, out[:n], nil
68+
outSmallCap := make([]byte, len(out))
69+
copy(outSmallCap, out)
70+
return n, outSmallCap, nil
6771
}
6872

6973
func (c *Compressor) compress(in, out []byte, f compress) (int, []byte, error) {

0 commit comments

Comments
 (0)