Skip to content

Commit

Permalink
[bulk] Fix being able to Decompress large payloads
Browse files Browse the repository at this point in the history
Contrary to Decompress, bulk.Decompress does not retry with stream decompression on failing to Decompress.

#115 introduced preventing to allocate too big buffers when the input zstd header was malicious. This had the side-effect that for highly compressed payloads (> 10x), the dst buffer was still resized and would fail.

This fixes the issue and adds a test
  • Loading branch information
Viq111 committed Aug 24, 2023
1 parent ea68dca commit 87ed960
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
6 changes: 3 additions & 3 deletions zstd_bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,20 @@ func (p *BulkProcessor) Decompress(dst, src []byte) ([]byte, error) {

contentSize := decompressSizeHint(src)
if cap(dst) >= contentSize {
dst = dst[0:contentSize]
dst = dst[0:cap(dst)]
} else {
dst = make([]byte, contentSize)
}

if contentSize == 0 {
if len(dst) == 0 {
return dst, nil
}

dctx := C.ZSTD_createDCtx()
cWritten := C.ZSTD_decompress_usingDDict(
dctx,
unsafe.Pointer(&dst[0]),
C.size_t(contentSize),
C.size_t(len(dst)),
unsafe.Pointer(&src[0]),
C.size_t(len(src)),
p.dDict,
Expand Down
25 changes: 25 additions & 0 deletions zstd_bullk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,31 @@ func TestBulkCompressAndDecompressInReverseOrder(t *testing.T) {
}
}

func TestDecompressHighlyCompressable(t *testing.T) {
p := newBulkProcessor(t, dict, BestSpeed)

// Generate a big payload
msgSize := 10 * 1000 * 1000 // 10 MiB
msg := make([]byte, msgSize)
compressed, err := Compress(nil, msg)
if err != nil {
t.Error("failed to compress")
}

// Regular decompression would trigger zipbomb prevention
_, err = p.Decompress(nil, compressed)
if !IsDstSizeTooSmallError(err) {
t.Error("expected too small error")
}

// Passing an output should suceed the decompression
dst := make([]byte, 10*msgSize)
_, err = p.Decompress(dst, compressed)
if err != nil {
t.Errorf("failed to decompress: %s", err)
}
}

// BenchmarkBulkCompress-8 780148 1505 ns/op 61.14 MB/s 208 B/op 5 allocs/op
func BenchmarkBulkCompress(b *testing.B) {
p := newBulkProcessor(b, dict, BestSpeed)
Expand Down

0 comments on commit 87ed960

Please sign in to comment.