Skip to content

Commit

Permalink
huff0: Prevent single 4X blocks exceeding 16 bits (#507)
Browse files Browse the repository at this point in the history
When used with max sized blocks, make sure the "jump table" can contain each output size.

zstd cannot trigger this, since max block size is half.
  • Loading branch information
klauspost committed Feb 28, 2022
1 parent 0f14e81 commit 2982376
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions huff0/compress.go
Expand Up @@ -2,6 +2,7 @@ package huff0

import (
"fmt"
"math"
"runtime"
"sync"
)
Expand Down Expand Up @@ -289,6 +290,10 @@ func (s *Scratch) compress4X(src []byte) ([]byte, error) {
if err != nil {
return nil, err
}
if len(s.Out)-idx > math.MaxUint16 {
// We cannot store the size in the jump table
return nil, ErrIncompressible
}
// Write compressed length as little endian before block.
if i < 3 {
// Last length is not written.
Expand Down Expand Up @@ -332,6 +337,10 @@ func (s *Scratch) compress4Xp(src []byte) ([]byte, error) {
return nil, errs[i]
}
o := s.tmpOut[i]
if len(o) > math.MaxUint16 {
// We cannot store the size in the jump table
return nil, ErrIncompressible
}
// Write compressed length as little endian before block.
if i < 3 {
// Last length is not written.
Expand Down
Binary file modified huff0/testdata/regression.zip
Binary file not shown.

0 comments on commit 2982376

Please sign in to comment.