JuliusBrussee/caveman · error

gzip read

Error message

gzip read: %w

What it means

DecompressLimit wraps any error returned while reading the inflated gzip stream with the prefix 'gzip read:'. The library throws it because io.ReadAll on the gzip reader failed mid-decompression, before the size limit check could even run. It signals corrupt or truncated gzip input rather than an over-limit payload (that case yields ErrDecompressedTooLarge).

Solutions

  1. Verify the data was actually compressed with gzip (check the 0x1f 0x8b magic header) before calling DecompressLimit with CompressionGzip.
  2. Re-upload or re-fetch the object; the stored bytes are likely truncated or corrupted.
  3. Check that the compressor used the same codec recorded in metadata (gzip vs zstd vs none) and fix the codec label.
  4. If wrapping a network/object read, ensure the full body was read and closed before decompression.

Example fix

// before
out, err := objectstore.DecompressLimit(data, objectstore.CompressionGzip, 1<<20) // data is raw (uncompressed) JSON
// after
if len(data) < 2 || data[0] != 0x1f || data[1] != 0x8b {
    return fmt.Errorf("payload is not gzip; check stored codec")
}
out, err := objectstore.DecompressLimit(data, objectstore.CompressionGzip, 1<<20)
Defensive patterns

Strategy: validation

Validate before calling

func isGzip(b []byte) bool { return len(b) >= 2 && b[0] == 0x1f && b[1] == 0x8b }
// call DecompressLimit(CompressionGzip) only if isGzip(data)

Type guard

func isGzip(b []byte) bool { return len(b) >= 2 && b[0] == 0x1f && b[1] == 0x8b }

Try / catch

out, err := objectstore.DecompressLimit(data, objectstore.CompressionGzip, limit)
if err != nil {
    if errors.Is(err, objectstore.ErrDecompressedTooLarge) { /* size path */ }
    return fmt.Errorf("gzip payload unreadable (corrupt/truncated): %w", err)
}

Prevention

When it happens

Trigger: Calling Decompress/DecompressLimit with CompressionGzip on bytes that are not valid complete gzip data: truncated stream, corrupted middle bytes, non-gzip input passed as gzip, or an underlying reader error while inflating.

Common situations: Storing compressed blobs with an older codec setting and later decompressing as gzip; partial uploads to object storage; a caller compressing with a different library/level or concatenating streams incorrectly; objects corrupted by a failed migration.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/99e42cedcb8eaeb0. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/objectstore/compress.go:84

func DecompressLimit(codec string, data []byte, maxBytes int64) ([]byte, error) {
	if maxBytes < 0 || maxBytes > DefaultMaxDecompressedBytes {
		return nil, fmt.Errorf("invalid decompression limit %d", maxBytes)
	}
	switch codec {
	case CompressionNone, "":
		if int64(len(data)) > maxBytes {
			return nil, ErrDecompressedTooLarge
		}
		return data, nil
	case CompressionGzip:
		zr, err := gzip.NewReader(bytes.NewReader(data))
		if err != nil {
			return nil, fmt.Errorf("gzip reader: %w", err)
		}
		defer zr.Close()
		out, err := io.ReadAll(io.LimitReader(zr, maxBytes+1))
		if err != nil {
			return nil, fmt.Errorf("gzip read: %w", err)
		}
		if int64(len(out)) > maxBytes {
			return nil, ErrDecompressedTooLarge
		}
		return out, nil
	case CompressionZstd:
		// Zstd's smallest legal window is 1 KiB. Keep that parser minimum for
		// tiny valid payloads, then independently enforce the exact byte limit.
		memoryLimit := uint64(maxBytes)
		if memoryLimit < zstd.MinWindowSize {
			memoryLimit = zstd.MinWindowSize
		}
		zr, err := zstd.NewReader(nil,
			zstd.WithDecoderConcurrency(1),
			zstd.WithDecoderMaxMemory(memoryLimit),
		)
		if err != nil {
			return nil, fmt.Errorf("zstd reader: %w", err)

View on GitHub (pinned to 3ee70a1026)