Skip to content

Commit

Permalink
s2: Fix S2 best dictionary wrong encoding (#871)
Browse files Browse the repository at this point in the history
A dictionary match one byte out of dictionary range was possible.

Recovery is possible. Send me a request if you find any content with this error in the [discussion](https://github.com/klauspost/compress/discussions/categories/general).
  • Loading branch information
klauspost authored Oct 13, 2023
1 parent 85bbf19 commit 68eabfa
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion s2/dict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,14 @@ func TestDictSize(t *testing.T) {
}
})
}
fmt.Printf("%d files, %d -> %d (%.2f%%) - %.02f bytes saved/file\n", totalCount, totalIn, totalOut, float64(totalOut*100)/float64(totalIn), float64(totalIn-totalOut)/float64(totalCount))
t.Logf("%d files, %d -> %d (%.2f%%) - %.02f bytes saved/file\n", totalCount, totalIn, totalOut, float64(totalOut*100)/float64(totalIn), float64(totalIn-totalOut)/float64(totalCount))
}

func FuzzDictBlocks(f *testing.F) {
fuzz.AddFromZip(f, "testdata/enc_regressions.zip", fuzz.TypeRaw, false)
fuzz.AddFromZip(f, "testdata/fuzz/block-corpus-raw.zip", fuzz.TypeRaw, testing.Short())
fuzz.AddFromZip(f, "testdata/fuzz/block-corpus-enc.zip", fuzz.TypeGoFuzz, testing.Short())
fuzz.AddFromZip(f, "testdata/fuzz/dict-corpus-oss.zip", fuzz.TypeOSSFuzz, testing.Short())

// Fuzzing tweaks:
const (
Expand Down
3 changes: 3 additions & 0 deletions s2/encode_best.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ func encodeBlockBest(dst, src []byte, dict *Dict) (d int) {
return m
}
matchDict := func(candidate, s int, first uint32, rep bool) match {
if s >= MaxDictSrcOffset {
return match{offset: candidate, s: s}
}
// Calculate offset as if in continuous array with s
offset := -len(dict.dict) + candidate
if best.length != 0 && best.s-best.offset == s-offset && !rep {
Expand Down
Binary file added s2/testdata/fuzz/dict-corpus-oss.zip
Binary file not shown.

0 comments on commit 68eabfa

Please sign in to comment.