Skip to content

Commit

Permalink
Always check decompression limit.
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Aug 20, 2019
1 parent b7017cd commit f4dc038
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion fse/decompress.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,12 @@ func (s *Scratch) decompress() error {
tmp[off+2] = s1.nextFast()
tmp[off+3] = s2.nextFast()
off += 4
// When off is 0, we have overflowed and should write.
if off == 0 {
s.Out = append(s.Out, tmp...)
if len(s.Out) >= s.DecompressLimit {
return fmt.Errorf("output size (%d) > DecompressLimit (%d)", len(s.Out), s.DecompressLimit)
}
}
}
} else {
Expand All @@ -296,7 +300,7 @@ func (s *Scratch) decompress() error {
off += 4
if off == 0 {
s.Out = append(s.Out, tmp...)
off = 0
// When off is 0, we have overflowed and should write.
if len(s.Out) >= s.DecompressLimit {
return fmt.Errorf("output size (%d) > DecompressLimit (%d)", len(s.Out), s.DecompressLimit)
}
Expand Down
2 changes: 2 additions & 0 deletions fse/fse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var decTestfiles = []struct {
{name: "crash4", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/crash4.bin") }, err: "symbolLen (1) too small"},
{name: "crash5", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/crash5.bin") }, err: "symbolLen (1) too small"},
{name: "crash6", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/dec-crash6.bin") }, err: "newState (32768) outside table size (32768)"},
{name: "something", fn: func() ([]byte, error) { return ioutil.ReadFile("../testdata/fse-artifact3.bin") }, err: "output size (1048576) > DecompressLimit (1048576)"},
}

func TestCompress(t *testing.T) {
Expand Down Expand Up @@ -107,6 +108,7 @@ func TestDecompress(t *testing.T) {
for _, test := range decTestfiles {
t.Run(test.name, func(t *testing.T) {
var s Scratch
s.DecompressLimit = 1 << 20
buf0, err := test.fn()
if err != nil {
t.Fatal(err)
Expand Down
Binary file added testdata/fse-artifact3.bin
Binary file not shown.

0 comments on commit f4dc038

Please sign in to comment.