Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for issue 125 - lz4 data corruption when concurrency is used #127

Merged
merged 5 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ func (z *Writer) writeHeader() error {

// Write compresses data from the supplied buffer into the underlying io.Writer.
// Write does not return until the data has been written.
func (z *Writer) Write(buf []byte) (int, error) {
func (z *Writer) Write(buffer []byte) (int, error) {
buf := make([]byte, len(buffer))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying here is not required.

copy(buf, buffer)

if !z.Header.done {
if err := z.writeHeader(); err != nil {
return 0, err
Expand Down Expand Up @@ -223,7 +226,10 @@ func (z *Writer) Write(buf []byte) (int, error) {
}

// compressBlock compresses a block.
func (z *Writer) compressBlock(data []byte) error {
func (z *Writer) compressBlock(dataBlock []byte) error {
data := make([]byte, len(dataBlock))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move it up before this is called and use the getBuffer function call,.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I missed this comment, it seems we might be able to use those pools that are already defined

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated it, I am new to the code and didn't know there were already pools defined for each block size :)

copy(data, dataBlock)

if !z.NoChecksum {
_, _ = z.checksum.Write(data)
}
Expand Down
58 changes: 58 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lz4_test

import (
"bufio"
"bytes"
"fmt"
"io"
Expand Down Expand Up @@ -183,3 +184,60 @@ func TestIssue71(t *testing.T) {
})
}
}

func TestIssue125(t *testing.T) {
goldenFiles := []string{
"testdata/e.txt",
"testdata/gettysburg.txt",
"testdata/Mark.Twain-Tom.Sawyer.txt",
"testdata/Mark.Twain-Tom.Sawyer_long.txt",
"testdata/pg1661.txt",
"testdata/pi.txt",
"testdata/random.data",
"testdata/repeat.txt",
"testdata/vmlinux_LZ4_19377",
}

for _, fname := range goldenFiles {
for _, concurrency := range []int{0, 4, -1} {
label := fmt.Sprintf("%s(%d)", fname, concurrency)
t.Run(label, func(t *testing.T) {
t.Parallel()

// this issue doesn't happen when read the entire file into a []byte
// and then use bytes.NewReader() since it actually does a copy on its
// own for each call to Read(), so we use *os.File directly with Copy()
f, err := os.Open(fname)
if err != nil {
t.Fatal(err)
}
defer f.Close()

var out bytes.Buffer
writer := lz4.NewWriter(&out).WithConcurrency(concurrency)
writer.Header = lz4.Header{
// we use a small enough block size so it is easier to trigger the issue
// as most examples files are small
BlockMaxSize: 1024 * 64,
}

_, err = io.Copy(writer, f)
if err != nil {
t.Fatal(err)
}
writer.Close()

reader := lz4.NewReader(&out)

var d bytes.Buffer
decompressed := bufio.NewWriter(&d)

_, err = io.Copy(decompressed, reader)
if err != nil {
t.Fail()
t.Log(err)
}
})
}
}
}