{"record":{"id":"dbc2fd7e5aa22f3d","repo":"JuliusBrussee/caveman","slug":"invalid-decompression-limit-d","errorCode":null,"errorMessage":"invalid decompression limit %d","messagePattern":"invalid decompression limit (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/objectstore/compress.go","lineNumber":68,"sourceCode":"\t\treturn zstdEnc.EncodeAll(data, nil), nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unknown compression codec %q\", codec)\n\t}\n}\n\n// Decompress reverses Compress with a defensive default expansion ceiling. An\n// unknown codec fails closed.\nfunc Decompress(codec string, data []byte) ([]byte, error) {\n\treturn DecompressLimit(codec, data, DefaultMaxDecompressedBytes)\n}\n\n// DecompressLimit reverses Compress while refusing to produce more than\n// maxBytes of plaintext. The limit is applied while decoding gzip and through\n// zstd's decoder memory bound, so highly-compressible hostile payloads cannot\n// first allocate their full expanded size and only then be rejected.\nfunc 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 {","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/objectstore/compress.go#L50-L86","documentation":"DecompressLimit was called with a maxBytes ceiling that is negative or larger than DefaultMaxDecompressedBytes. The library enforces a global upper bound on decompressed output to prevent decompression-bomb memory exhaustion, so out-of-range limits are rejected up front with this error before any decoding begins. The %d shows the offending value.","triggerScenarios":"Calling DecompressLimit with maxBytes < 0, or maxBytes > DefaultMaxDecompressedBytes — e.g. passing -1 as 'unlimited', reading a limit from config as raw int64 without bounds, or arithmetic overflow producing a huge value.","commonSituations":"Developers attempting to disable the limit by passing math.MaxInt64 or -1, config values in bytes misinterpreted (GB value fed as byte count), or a caller converting a user-supplied 'max size in MB' incorrectly.","solutions":["Clamp the limit into the valid range: 0 <= maxBytes <= DefaultMaxDecompressedBytes.","Do not attempt to disable the ceiling; instead decompress in chunks or use a streaming decoder if you need more than the default.","Validate config-derived limits at startup, failing fast on out-of-range values.","Check for arithmetic bugs (unit conversion, int overflow) producing the bad value.","Use Decompress (which applies the default ceiling) when you have no specific size requirement."],"exampleFix":"// before\nout, err := objectstore.DecompressLimit(codec, data, -1) // invalid decompression limit -1\n// after\nlimit := int64(cfg.MaxDecompressedMB) << 20\nif limit <= 0 || limit > objectstore.DefaultMaxDecompressedBytes {\n    limit = objectstore.DefaultMaxDecompressedBytes\n}\nout, err := objectstore.DecompressLimit(codec, data, limit)","handlingStrategy":"validation","validationCode":"if maxBytes < 0 || maxBytes > objectstore.DefaultMaxDecompressedBytes {\n    maxBytes = objectstore.DefaultMaxDecompressedBytes\n}\nout, err := objectstore.DecompressLimit(codec, data, maxBytes)","typeGuard":null,"tryCatchPattern":"if err != nil {\n    if strings.HasPrefix(err.Error(), \"invalid decompression limit\") {\n        // clamp and retry once rather than failing the operation\n        out, err = objectstore.DecompressLimit(codec, data, objectstore.DefaultMaxDecompressedBytes)\n    }\n    if err != nil {\n        return err\n    }\n}","preventionTips":["Never use -1 or math.MaxInt64 to mean 'unlimited'; use Decompress for the default ceiling.","Validate size limits at startup with explicit unit conversions (MB -> bytes).","Centralize limit derivation in one helper so bounds are checked once.","Add unit tests for boundary values (0, DefaultMaxDecompressedBytes, -1)."],"tags":["compression","validation","limits","go"],"backgroundTag":"argument-out-of-range","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"}