Skip to content

Commit

Permalink
Fix inconsistent zstd error (#179)
Browse files Browse the repository at this point in the history
When a data ended exactly where a block is supposed to start, always return io.ErrUnexpectedEOF
  • Loading branch information
klauspost committed Nov 10, 2019
1 parent f741997 commit fb5147c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
3 changes: 2 additions & 1 deletion zstd/blockdec.go
Expand Up @@ -161,7 +161,8 @@ func (b *blockDec) reset(br byteBuffer, windowSize uint64) error {
b.data, err = br.readBig(cSize, b.dataStorage)
if err != nil {
if debug {
println("Reading block:", err)
println("Reading block:", err, "(", cSize, ")", len(b.data))
printf("%T", br)
}
return err
}
Expand Down
3 changes: 3 additions & 0 deletions zstd/bytebuf.go
Expand Up @@ -101,6 +101,9 @@ func (r *readerWrapper) readBig(n int, dst []byte) ([]byte, error) {
dst = make([]byte, n)
}
n2, err := io.ReadFull(r.r, dst[:n])
if err == io.EOF && n > 0 {
err = io.ErrUnexpectedEOF
}
return dst[:n2], err
}

Expand Down
40 changes: 39 additions & 1 deletion zstd/decoder_test.go
Expand Up @@ -342,7 +342,7 @@ func TestNewDecoderFlushed(t *testing.T) {
}

func TestDecoderRegression(t *testing.T) {
defer timeout(60 * time.Second)()
defer timeout(160 * time.Second)()
data, err := ioutil.ReadFile("testdata/regression.zip")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -443,6 +443,44 @@ func TestDecoderRegression(t *testing.T) {
}
}
})
t.Run("Match-"+tt.Name, func(t *testing.T) {
r, err := tt.Open()
if err != nil {
t.Error(err)
return
}
in, err := ioutil.ReadAll(r)
if err != nil {
t.Error(err)
}
got, gotErr := dec.DecodeAll(in, nil)
t.Log("Received:", len(got), gotErr)

// Check a fresh instance
decL, err := NewReader(bytes.NewBuffer(in), WithDecoderConcurrency(1), WithDecoderLowmem(true), WithDecoderMaxMemory(1<<20))
if err != nil {
t.Error(err)
return
}
defer decL.Close()
got2, gotErr2 := ioutil.ReadAll(decL)
t.Log("Reader Reader received:", len(got2), gotErr2)
if gotErr != gotErr2 {
if gotErr != nil && gotErr2 != nil && gotErr.Error() != gotErr2.Error() {
t.Error(gotErr, "!=", gotErr2)
}
if (gotErr == nil) != (gotErr2 == nil) {
t.Error(gotErr, "!=", gotErr2)
}
}
if !bytes.Equal(got2, got) {
if gotErr != nil {
t.Log("Buffer mismatch")
} else {
t.Error("Buffer mismatch")
}
}
})
}
}

Expand Down
Binary file modified zstd/testdata/regression.zip
Binary file not shown.

0 comments on commit fb5147c

Please sign in to comment.