Browse Source

#42: the compress functions can write pass the end of the slice via copy. Make sure the capacity of the buffer used for compression is the same as its length.

Pierre Curto 6 years ago
parent
commit
6749706c16
4 changed files with 31 additions and 5 deletions
  1. 3 3
      reader.go
  2. BIN
      testdata/issue42.data
  3. 2 2
      writer.go
  4. 26 0
      writer_test.go

+ 3 - 3
reader.go

@@ -101,7 +101,7 @@ func (z *Reader) readHeader(first bool) error {
 	z.data = z.zdata[:cap(z.zdata)][bSize:]
 	z.data = z.zdata[:cap(z.zdata)][bSize:]
 	z.idx = len(z.data)
 	z.idx = len(z.data)
 
 
-	z.checksum.Write(buf[0:2])
+	_, _ = z.checksum.Write(buf[0:2])
 
 
 	if frameSize {
 	if frameSize {
 		buf := buf[:8]
 		buf := buf[:8]
@@ -110,7 +110,7 @@ func (z *Reader) readHeader(first bool) error {
 		}
 		}
 		z.Size = binary.LittleEndian.Uint64(buf)
 		z.Size = binary.LittleEndian.Uint64(buf)
 		z.pos += 8
 		z.pos += 8
-		z.checksum.Write(buf)
+		_, _ = z.checksum.Write(buf)
 	}
 	}
 
 
 	// Header checksum.
 	// Header checksum.
@@ -258,7 +258,7 @@ func (z *Reader) Read(buf []byte) (int, error) {
 		}
 		}
 
 
 		if !z.NoChecksum {
 		if !z.NoChecksum {
-			z.checksum.Write(z.data)
+			_, _ = z.checksum.Write(z.data)
 			if debugFlag {
 			if debugFlag {
 				debug("current frame checksum %x", z.checksum.Sum32())
 				debug("current frame checksum %x", z.checksum.Sum32())
 			}
 			}

BIN
testdata/issue42.data


+ 2 - 2
writer.go

@@ -46,8 +46,8 @@ func (z *Writer) writeHeader() error {
 	if n := 2 * bSize; cap(z.zdata) < n {
 	if n := 2 * bSize; cap(z.zdata) < n {
 		z.zdata = make([]byte, n, 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
 	z.idx = 0
 
 
 	// Size is optional.
 	// Size is optional.

+ 26 - 0
writer_test.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"fmt"
 	"io"
 	"io"
 	"io/ioutil"
 	"io/ioutil"
+	"os"
 	"reflect"
 	"reflect"
 	"testing"
 	"testing"
 
 
@@ -96,3 +97,28 @@ func TestIssue41(t *testing.T) {
 		t.Fatal("uncompressed data does not match original")
 		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)
+	}
+}