Skip to content

Commit

Permalink
huff0: Return compression error. (#172)
Browse files Browse the repository at this point in the history
* huff0: Return compression error.
* Add test that matches the fuzz test.

When reusing, errors returned from the compressor was not forwarded correctly.
  • Loading branch information
klauspost authored Oct 24, 2019
1 parent 1fdf147 commit 169bb21
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
3 changes: 3 additions & 0 deletions huff0/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ func compress(in []byte, s *Scratch, compressor func(src []byte) ([]byte, error)
keepTable := s.cTable
s.cTable = s.prevTable
s.Out, err = compressor(in)
if err != nil {
return nil, false, err
}
s.cTable = keepTable
if len(s.Out) >= len(in) {
return nil, false, ErrIncompressible
Expand Down
114 changes: 114 additions & 0 deletions huff0/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,120 @@ func init() {
}
}

func TestCompressRegression(t *testing.T) {
// Match the fuzz function
var testInput = func(data []byte) int {
var sc Scratch
comp, _, err := Compress1X(data, &sc)
if err == ErrIncompressible || err == ErrUseRLE || err == ErrTooBig {
return 0
}
if err != nil {
panic(err)
}
s, remain, err := ReadTable(comp, nil)
if err != nil {
panic(err)
}
out, err := s.Decompress1X(remain)
if err != nil {
panic(err)
}
if !bytes.Equal(out, data) {
panic("decompression 1x mismatch")
}
// Reuse as 4X
sc.Reuse = ReusePolicyAllow
comp, reUsed, err := Compress4X(data, &sc)
if err == ErrIncompressible || err == ErrUseRLE || err == ErrTooBig {
return 0
}
if err != nil {
panic(err)
}
remain = comp
if !reUsed {
s, remain, err = ReadTable(comp, s)
if err != nil {
panic(err)
}
}
out, err = s.Decompress4X(remain, len(data))
if err != nil {
panic(err)
}
if !bytes.Equal(out, data) {
panic("decompression 4x with reuse mismatch")
}

s.Reuse = ReusePolicyNone
comp, reUsed, err = Compress4X(data, s)
if err == ErrIncompressible || err == ErrUseRLE || err == ErrTooBig {
return 0
}
if err != nil {
panic(err)
}
if reUsed {
panic("reused when asked not to")
}
s, remain, err = ReadTable(comp, nil)
if err != nil {
panic(err)
}
out, err = s.Decompress4X(remain, len(data))
if err != nil {
panic(err)
}
if !bytes.Equal(out, data) {
panic("decompression 4x mismatch")
}

// Reuse as 1X
s.Reuse = ReusePolicyAllow
comp, reUsed, err = Compress1X(data, &sc)
if err == ErrIncompressible || err == ErrUseRLE || err == ErrTooBig {
return 0
}
if err != nil {
panic(err)
}
remain = comp
if !reUsed {
s, remain, err = ReadTable(comp, s)
if err != nil {
panic(err)
}
}
out, err = s.Decompress1X(remain)
if err != nil {
panic(err)
}
if !bytes.Equal(out, data) {
panic("decompression 1x with reuse mismatch")
}
return 1
}
for _, test := range testfiles {
t.Run(test.name, func(t *testing.T) {
buf0, err := test.fn()
if err != nil {
t.Fatal(err)
}
testInput(buf0)
})
}
for _, test := range testfilesExtended {
t.Run(test.name, func(t *testing.T) {
buf0, err := test.fn()
if err != nil {
t.Fatal(err)
}
testInput(buf0)
})
}
}

func TestCompress1X(t *testing.T) {
for _, test := range testfiles {
t.Run(test.name, func(t *testing.T) {
Expand Down
Binary file modified huff0/testdata/regression.zip
Binary file not shown.

0 comments on commit 169bb21

Please sign in to comment.