{"record":{"id":"d76208fd1e6ec994","repo":"grpc/grpc-go","slug":"grpc-invalid-gzip-compression-level-d","errorCode":null,"errorMessage":"grpc: invalid gzip compression level: %d","messagePattern":"grpc: invalid gzip compression level: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"encoding/gzip/gzip.go","lineNumber":60,"sourceCode":"\tc.poolCompressor.New = func() any {\n\t\treturn &writer{Writer: gzip.NewWriter(io.Discard), pool: &c.poolCompressor}\n\t}\n\tencoding.RegisterCompressor(c)\n}\n\ntype writer struct {\n\t*gzip.Writer\n\tpool *sync.Pool\n}\n\n// SetLevel updates the registered gzip compressor to use the compression level specified (gzip.HuffmanOnly is not supported).\n// NOTE: this function must only be called during initialization time (i.e. in an init() function),\n// and is not thread-safe.\n//\n// The error returned will be nil if the specified level is valid.\nfunc SetLevel(level int) error {\n\tif level < gzip.DefaultCompression || level > gzip.BestCompression {\n\t\treturn fmt.Errorf(\"grpc: invalid gzip compression level: %d\", level)\n\t}\n\tc := encoding.GetCompressor(Name).(*compressor)\n\tc.poolCompressor.New = func() any {\n\t\tw, err := gzip.NewWriterLevel(io.Discard, level)\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\treturn &writer{Writer: w, pool: &c.poolCompressor}\n\t}\n\treturn nil\n}\n\nfunc (c *compressor) Compress(w io.Writer) (io.WriteCloser, error) {\n\tz := c.poolCompressor.Get().(*writer)\n\tz.Writer.Reset(w)\n\treturn z, nil\n}\n","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/grpc/grpc-go/blob/03255a9237b6eb32710f6bc4f2de9a675b99fe36/encoding/gzip/gzip.go#L42-L78","documentation":"gzip.SetLevel rejects a compression level outside the standard library range [gzip.DefaultCompression (-1) .. gzip.BestCompression (9)]. The function documents that gzip.HuffmanOnly (-2) is also unsupported. The check is `level < gzip.DefaultCompression || level > gzip.BestCompression`.","triggerScenarios":"Calling gzip.SetLevel(level) at init time with level < -1 (including HuffmanOnly -2) or level > 9. Common when a configurable env var or flag is parsed to an int and forwarded unvalidated.","commonSituations":"Reading compression level from a config/flag that defaulted to 0 (which is valid and means no compression) vs an out-of-range sentinel; passing a negative number to mean 'disabled'; copy-pasting a constant from another library.","solutions":["Use a level in [gzip.DefaultCompression, gzip.BestCompression] = [-1, 9]; common choices are gzip.DefaultCompression (-1), gzip.BestSpeed (1), gzip.BestCompression (9).","If you want Huffman-only, know that this grpc gzip package deliberately does not support it; pick level 1 (BestSpeed) instead.","Validate the level from config before calling SetLevel and fall back to a sane default (e.g. gzip.DefaultCompression)."],"exampleFix":"// before\nif err := gzip.SetLevel(-2); err != nil { log.Fatal(err) } // invalid\n\n// after\nif err := gzip.SetLevel(gzip.BestSpeed); err != nil { log.Fatal(err) }","handlingStrategy":"validation","validationCode":"import \"compress/gzip\"\n\nfunc validGzipLevel(level int) error {\n    if level < gzip.DefaultCompression || level > gzip.BestCompression {\n        return fmt.Errorf(\"gzip level %d out of range [-1,9]\", level)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := gzip.SetLevel(level); err != nil {\n    return fmt.Errorf(\"bad gzip level %d: %w\", level, err)\n}","preventionTips":["Constrain configurable compression level to [-1, 9].","Remember this package rejects gzip.HuffmanOnly (-2); use BestSpeed (1) instead.","Call SetLevel only from init(), never concurrently."],"tags":["grpc","gzip","compression","encoding","init","validation"],"analyzedSha":"03255a9237b6eb32710f6bc4f2de9a675b99fe36","analyzedAt":"2026-08-07T00:29:34.215Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}