JuliusBrussee/caveman · error

zstd read

Error message

zstd read: %w

What it means

DecompressLimit wraps zstd.DecodeAll failures with 'zstd read:' when the error is not the recognized size/window limit errors (those are translated to ErrDecompressedTooLarge). It means the zstd payload itself failed to decode: corrupt, truncated, or not zstd data.

Solutions

  1. Verify the payload magic (zstd frames start with 0x28 0xB5 0x2F 0xFD) and that the stored codec label matches.
  2. Re-fetch/re-upload the object; the stored bytes are likely truncated or corrupted.
  3. Confirm the producer used the same zstd library/framing (klauspost/compress DecodeAll expects standard frames).
  4. Test the same blob with an independent zstd decoder (e.g. `zstd -t file`) to confirm corruption.

Example fix

// before
out, err := objectstore.DecompressLimit(blob, objectstore.CompressionZstd, limit) // blob is gzip, metadata says zstd
// after
if len(blob) < 4 || binary.LittleEndian.Uint32(blob) != 0xFD2FB528 {
    return fmt.Errorf("blob is not zstd; fix codec metadata")
}
out, err := objectstore.DecompressLimit(blob, objectstore.CompressionZstd, limit)
Defensive patterns

Strategy: validation

Validate before calling

func isZstd(b []byte) bool { return len(b) >= 4 && binary.LittleEndian.Uint32(b) == 0xFD2FB528 }

Type guard

func isZstd(b []byte) bool { return len(b) >= 4 && binary.LittleEndian.Uint32(b) == 0xFD2FB528 }

Try / catch

out, err := objectstore.DecompressLimit(data, objectstore.CompressionZstd, limit)
switch {
case errors.Is(err, objectstore.ErrDecompressedTooLarge): // over-limit path
case err != nil: return fmt.Errorf("zstd payload corrupt or wrong codec: %w", err)
}

Prevention

When it happens

Trigger: Calling Decompress/DecompressLimit with CompressionZstd on bytes that are not a valid complete zstd frame — wrong codec label, truncated upload, bit rot, or data compressed with a different algorithm.

Common situations: Objects written by another tool with a raw/legacy zstd framing; metadata says zstd but body is gzip or plain; interrupted multipart uploads; disk corruption.

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/b1bc80bc3de3813d. Report an issue: GitHub.

Appendix: source

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

		// 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)
		}
		defer zr.Close()
		out, err := zr.DecodeAll(data, nil)
		if err != nil {
			if errors.Is(err, zstd.ErrDecoderSizeExceeded) || errors.Is(err, zstd.ErrWindowSizeExceeded) {
				return nil, ErrDecompressedTooLarge
			}
			return nil, fmt.Errorf("zstd read: %w", err)
		}
		if int64(len(out)) > maxBytes {
			return nil, ErrDecompressedTooLarge
		}
		return out, nil
	default:
		return nil, fmt.Errorf("unknown compression codec %q", codec)
	}
}

View on GitHub (pinned to 3ee70a1026)