fix: prevent double-close of blobgen.Writer in CompressStream
CompressStream had both a defer w.Close() and an explicit w.Close() call, causing the compressor and encryptor to be closed twice. The second close on the zstd encoder returns an error, and the age encryptor may write duplicate finalization bytes, potentially corrupting the output stream. Use a closed flag to prevent the deferred close from running after the explicit close succeeds.
This commit is contained in:
parent
46c2ea3079
commit
441c441eca
@ -51,7 +51,13 @@ func CompressStream(dst io.Writer, src io.Reader, compressionLevel int, recipien
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, "", fmt.Errorf("creating writer: %w", err)
|
return 0, "", fmt.Errorf("creating writer: %w", err)
|
||||||
}
|
}
|
||||||
defer func() { _ = w.Close() }()
|
|
||||||
|
closed := false
|
||||||
|
defer func() {
|
||||||
|
if !closed {
|
||||||
|
_ = w.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// Copy data
|
// Copy data
|
||||||
if _, err := io.Copy(w, src); err != nil {
|
if _, err := io.Copy(w, src); err != nil {
|
||||||
@ -62,6 +68,7 @@ func CompressStream(dst io.Writer, src io.Reader, compressionLevel int, recipien
|
|||||||
if err := w.Close(); err != nil {
|
if err := w.Close(); err != nil {
|
||||||
return 0, "", fmt.Errorf("closing writer: %w", err)
|
return 0, "", fmt.Errorf("closing writer: %w", err)
|
||||||
}
|
}
|
||||||
|
closed = true
|
||||||
|
|
||||||
return w.BytesWritten(), hex.EncodeToString(w.Sum256()), nil
|
return w.BytesWritten(), hex.EncodeToString(w.Sum256()), nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user