Skip to content

Commit

Permalink
Check sizes compared to header sizes.
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Aug 17, 2023
1 parent dbdb94e commit 1ca482f
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion zstd/blockenc.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,14 @@ func (b *blockEnc) encodeLits(lits []byte, raw bool) error {
} else {
err = huff0.ErrIncompressible
}

if err == nil && len(out)+5 > len(lits) {
// If we are close, we may still be worse or equal to raw.
var lh literalsHeader
lh.setSizes(len(out), len(lits), single)
if len(out)+lh.size() >= len(lits) {
err = huff0.ErrIncompressible
}
}
switch err {
case huff0.ErrIncompressible:
if debugEncoder {
Expand Down Expand Up @@ -511,6 +518,17 @@ func (b *blockEnc) encode(org []byte, raw, rawAllLits bool) error {
err = huff0.ErrIncompressible
}

if err == nil && len(out)+5 > len(b.literals) {
// If we are close, we may still be worse or equal to raw.
var lh literalsHeader
lh.setSize(len(b.literals))
szRaw := lh.size()
lh.setSizes(len(out), len(b.literals), single)
szComp := lh.size()
if len(out)+szComp >= len(b.literals)+szRaw {
err = huff0.ErrIncompressible
}
}
switch err {
case huff0.ErrIncompressible:
lh.setType(literalsBlockRaw)
Expand Down

0 comments on commit 1ca482f

Please sign in to comment.