{"record":{"id":"19a6a5e871a0b0e6","repo":"golang/go","slug":"zlib-invalid-compression-level-d","errorCode":null,"errorMessage":"zlib: invalid compression level: %d","messagePattern":"zlib: invalid compression level: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/compress/zlib/writer.go","lineNumber":78,"sourceCode":"// Note that the exact bytes written to w are not covered by the Go 1\n// compatibility promise. Callers, including tests, should not depend on the\n// exact written bytes.\nfunc NewWriterLevel(w io.Writer, level int) (*Writer, error) {\n\treturn NewWriterLevelDict(w, level, nil)\n}\n\n// NewWriterLevelDict is like [NewWriterLevel] but specifies a dictionary to\n// compress with.\n//\n// The dictionary may be nil. If not, its contents should not be modified until\n// the Writer is closed.\n//\n// Note that the exact bytes written to w are not covered by the Go 1\n// compatibility promise. Callers, including tests, should not depend on the\n// exact written bytes.\nfunc NewWriterLevelDict(w io.Writer, level int, dict []byte) (*Writer, error) {\n\tif level < HuffmanOnly || level > BestCompression {\n\t\treturn nil, fmt.Errorf(\"zlib: invalid compression level: %d\", level)\n\t}\n\treturn &Writer{\n\t\tw:     w,\n\t\tlevel: level,\n\t\tdict:  dict,\n\t}, nil\n}\n\n// Reset clears the state of the [Writer] z such that it is equivalent to its\n// initial state from [NewWriterLevel] or [NewWriterLevelDict], but instead writing\n// to w.\nfunc (z *Writer) Reset(w io.Writer) {\n\tz.w = w\n\t// z.level and z.dict left unchanged.\n\tif z.compressor != nil {\n\t\tz.compressor.Reset(w)\n\t}\n\tif z.digest != nil {","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/compress/zlib/writer.go#L60-L96","documentation":"`zlib.NewWriterLevel` (and `NewWriterLevelDict`) reject a `level` outside `[HuffmanOnly(-2), BestCompression(9)]` before constructing the writer. This mirrors gzip/flate; zlib wraps DEFLATE so the level range is identical.","triggerScenarios":"Calling `zlib.NewWriterLevel(w, level)` with level < -2 or > 9; plumbing a config value on a different scale; passing the `zlib` level where a `flate` constant of a different numbering was intended (same range, but easy to mix).","commonSituations":"Settings files exposing a `level` field without bounds; defaulting to 0 thinking it means BestSpeed (0 is actually NoCompression/Store for flate-backed zlib — valid, but surprising — whereas the invalid values are < -2 or > 9).","solutions":["Clamp into range using the zlib constants (`zlib.DefaultCompression`, `zlib.BestSpeed`, `zlib.BestCompression`, `zlib.HuffmanOnly`).","If unsure, use `zlib.DefaultCompression` (-1).","Validate config-supplied integers at load time and reject with a clear message."],"exampleFix":"// before\nzw, err := zlib.NewWriterLevel(out, 12)\n// after\nzw, err := zlib.NewWriterLevel(out, zlib.BestCompression)","handlingStrategy":"validation","validationCode":"if level < zlib.HuffmanOnly || level > zlib.BestCompression {\n    level = zlib.DefaultCompression\n}","typeGuard":null,"tryCatchPattern":"zw, err := zlib.NewWriterLevel(out, level)\nif err != nil { zw = zlib.NewWriter(out) /* fall back to default */ }","preventionTips":["Use zlib.DefaultCompression if you have no specific need.","Bound-check any config-derived level before calling NewWriterLevel."],"tags":["compress","zlib","argument-validation","configuration"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}