You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When invoking .Close() multiple times on a zstd.(*Encoder) instance, the CRC checksum is written to the underlying stream multiple times. This behavior prevents reliable use of defer zstdWr.Close() in conjunction with an explicit zstdWr.Close() mid-function, as it results in duplicate checksum entries.
Other io.WriteCloser implementations, such as Go's standard library gzip.(*Writer).Close(), prevent this issue by setting a closed flag internally, ensuring the Close() method is idempotent.
Steps to Reproduce:
package main
import (
"bytes""crypto/rand""github.com/klauspost/compress/zstd"
)
funcmain() {
buf:=bytes.NewBuffer(nil)
encoder, _:=zstd.NewWriter(buf)
b:=make([]byte, 131072)
rand.Read(b)
encoder.Write(b)
encoder.Close() // First closeencoder.Close() // Second close (shouldn't write checksum again)b=buf.Bytes()
// Compare the last two 4-byte sequences in the buffer (which are the checksums)c1:=b[len(b)-4:]
c2:=b[len(b)-8 : len(b)-4]
ifstring(c1) ==string(c2) {
panic("Duplicate CRC checksums written")
}
}
The text was updated successfully, but these errors were encountered:
When invoking
.Close()
multiple times on azstd.(*Encoder)
instance, the CRC checksum is written to the underlying stream multiple times. This behavior prevents reliable use ofdefer zstdWr.Close()
in conjunction with an explicitzstdWr.Close()
mid-function, as it results in duplicate checksum entries.Other
io.WriteCloser
implementations, such as Go's standard librarygzip.(*Writer).Close()
, prevent this issue by setting a closed flag internally, ensuring theClose()
method is idempotent.Steps to Reproduce:
The text was updated successfully, but these errors were encountered: