Files
Nikolay fb2d323b06 lib: properly apply snappy Decode limits
Previously, snappy Decoder didn't take in account Request Size limits
applied by VictoriaMetrics components.  And in case of incorrectly formed snappy block, VictoriaMetrics
 component may allocate extra memory. Which may lead to the OOM errors.

This commit makes ingest endpoints check block size header based on MaxRequest Limits.
2025-11-04 13:04:27 +03:00

27 lines
849 B
Go

package snappy
import (
"fmt"
"github.com/golang/snappy"
)
// Decode returns the decoded form of src with provided max block data size.
// The returned slice may be a sub-slice of dst
// if dst was large enough to hold the entire decoded block.
// Otherwise, a newly allocated slice will be returned.
//
// The dst and src must not overlap. It is valid to pass a nil dst.
//
// Decode handles the Snappy block format, not the Snappy stream format.
func Decode(dst []byte, src []byte, maxDataSizeBytes int) ([]byte, error) {
dstLen, err := snappy.DecodedLen(src)
if err != nil {
return nil, fmt.Errorf("cannot read snappy header: %w", err)
}
if maxDataSizeBytes > 0 && dstLen > maxDataSizeBytes {
return nil, fmt.Errorf("too big data size %d exceeding %d bytes", dstLen, maxDataSizeBytes)
}
return snappy.Decode(dst[:cap(dst)], src)
}