{"record":{"id":"99e42cedcb8eaeb0","repo":"JuliusBrussee/caveman","slug":"gzip-read-w","errorCode":null,"errorMessage":"gzip read: %w","messagePattern":"gzip read: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/objectstore/compress.go","lineNumber":84,"sourceCode":"func DecompressLimit(codec string, data []byte, maxBytes int64) ([]byte, error) {\n\tif maxBytes < 0 || maxBytes > DefaultMaxDecompressedBytes {\n\t\treturn nil, fmt.Errorf(\"invalid decompression limit %d\", maxBytes)\n\t}\n\tswitch codec {\n\tcase CompressionNone, \"\":\n\t\tif int64(len(data)) > maxBytes {\n\t\t\treturn nil, ErrDecompressedTooLarge\n\t\t}\n\t\treturn data, nil\n\tcase CompressionGzip:\n\t\tzr, err := gzip.NewReader(bytes.NewReader(data))\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"gzip reader: %w\", err)\n\t\t}\n\t\tdefer zr.Close()\n\t\tout, err := io.ReadAll(io.LimitReader(zr, maxBytes+1))\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"gzip read: %w\", err)\n\t\t}\n\t\tif int64(len(out)) > maxBytes {\n\t\t\treturn nil, ErrDecompressedTooLarge\n\t\t}\n\t\treturn out, nil\n\tcase CompressionZstd:\n\t\t// Zstd's smallest legal window is 1 KiB. Keep that parser minimum for\n\t\t// tiny valid payloads, then independently enforce the exact byte limit.\n\t\tmemoryLimit := uint64(maxBytes)\n\t\tif memoryLimit < zstd.MinWindowSize {\n\t\t\tmemoryLimit = zstd.MinWindowSize\n\t\t}\n\t\tzr, err := zstd.NewReader(nil,\n\t\t\tzstd.WithDecoderConcurrency(1),\n\t\t\tzstd.WithDecoderMaxMemory(memoryLimit),\n\t\t)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"zstd reader: %w\", err)","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/objectstore/compress.go#L66-L102","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the data was actually compressed with gzip (check the 0x1f 0x8b magic header) before calling DecompressLimit with CompressionGzip.","Re-upload or re-fetch the object; the stored bytes are likely truncated or corrupted.","Check that the compressor used the same codec recorded in metadata (gzip vs zstd vs none) and fix the codec label.","If wrapping a network/object read, ensure the full body was read and closed before decompression."],"exampleFix":"// before\nout, err := objectstore.DecompressLimit(data, objectstore.CompressionGzip, 1<<20) // data is raw (uncompressed) JSON\n// after\nif len(data) < 2 || data[0] != 0x1f || data[1] != 0x8b {\n    return fmt.Errorf(\"payload is not gzip; check stored codec\")\n}\nout, err := objectstore.DecompressLimit(data, objectstore.CompressionGzip, 1<<20)","handlingStrategy":"validation","validationCode":"func isGzip(b []byte) bool { return len(b) >= 2 && b[0] == 0x1f && b[1] == 0x8b }\n// call DecompressLimit(CompressionGzip) only if isGzip(data)","typeGuard":"func isGzip(b []byte) bool { return len(b) >= 2 && b[0] == 0x1f && b[1] == 0x8b }","tryCatchPattern":"out, err := objectstore.DecompressLimit(data, objectstore.CompressionGzip, limit)\nif err != nil {\n    if errors.Is(err, objectstore.ErrDecompressedTooLarge) { /* size path */ }\n    return fmt.Errorf(\"gzip payload unreadable (corrupt/truncated): %w\", err)\n}","preventionTips":["Persist the codec used at compression time alongside the object and read it back instead of assuming.","Check the gzip magic header before decompressing.","Treat this error as data corruption: alert and re-ingest rather than retrying the same bytes."],"tags":["gzip","decompression","corrupt-data"],"backgroundTag":"file-read-failed","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}