Skip to content

Commit

Permalink
#42: the compress functions can write pass the end of the slice via c…
Browse files Browse the repository at this point in the history
…opy. Make sure the capacity of the buffer used for compression is the same as its length.
  • Loading branch information
Pierre Curto authored and Pierre Curto committed Apr 30, 2019
1 parent 9419d23 commit 6749706
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
6 changes: 3 additions & 3 deletions reader.go
Expand Up @@ -101,7 +101,7 @@ func (z *Reader) readHeader(first bool) error {
z.data = z.zdata[:cap(z.zdata)][bSize:]
z.idx = len(z.data)

z.checksum.Write(buf[0:2])
_, _ = z.checksum.Write(buf[0:2])

if frameSize {
buf := buf[:8]
Expand All @@ -110,7 +110,7 @@ func (z *Reader) readHeader(first bool) error {
}
z.Size = binary.LittleEndian.Uint64(buf)
z.pos += 8
z.checksum.Write(buf)
_, _ = z.checksum.Write(buf)
}

// Header checksum.
Expand Down Expand Up @@ -258,7 +258,7 @@ func (z *Reader) Read(buf []byte) (int, error) {
}

if !z.NoChecksum {
z.checksum.Write(z.data)
_, _ = z.checksum.Write(z.data)
if debugFlag {
debug("current frame checksum %x", z.checksum.Sum32())
}
Expand Down
Binary file added testdata/issue42.data
Binary file not shown.
4 changes: 2 additions & 2 deletions writer.go
Expand Up @@ -46,8 +46,8 @@ func (z *Writer) writeHeader() error {
if n := 2 * bSize; cap(z.zdata) < n {
z.zdata = make([]byte, n, n)
}
z.zdata = z.zdata[:bSize]
z.data = z.zdata[:cap(z.zdata)][bSize:]
z.data = z.zdata[:bSize]
z.zdata = z.zdata[:cap(z.zdata)][bSize:]
z.idx = 0

// Size is optional.
Expand Down
26 changes: 26 additions & 0 deletions writer_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"testing"

Expand Down Expand Up @@ -96,3 +97,28 @@ func TestIssue41(t *testing.T) {
t.Fatal("uncompressed data does not match original")
}
}

func TestIssue42(t *testing.T) {
r, w := io.Pipe()
go func() {
defer w.Close()

f, err := os.Open("testdata/issue42.data")
if err != nil {
t.Fatal(err)
}
defer f.Close()

zw := lz4.NewWriter(w)
defer zw.Close()

_, err = io.Copy(zw, f)
if err != nil {
t.Fatal(err)
}
}()
_, err := io.Copy(ioutil.Discard, lz4.NewReader(r))
if err != nil {
t.Fatal(err)
}
}

0 comments on commit 6749706

Please sign in to comment.