{"record":{"id":"f0c865100180ea45","repo":"golang/go","slug":"lzw-input-byte-too-large-for-the-litwidth","errorCode":null,"errorMessage":"lzw: input byte too large for the litWidth","messagePattern":"lzw: input byte too large for the litWidth","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/compress/lzw/writer.go","lineNumber":132,"sourceCode":"\t\t\tw.table[i] = invalidEntry\n\t\t}\n\t\treturn errOutOfCodes\n\t}\n\treturn nil\n}\n\n// Write writes a compressed representation of p to w's underlying writer.\nfunc (w *Writer) Write(p []byte) (n int, err error) {\n\tif w.err != nil {\n\t\treturn 0, w.err\n\t}\n\tif len(p) == 0 {\n\t\treturn 0, nil\n\t}\n\tif maxLit := uint8(1<<w.litWidth - 1); maxLit != 0xff {\n\t\tfor _, x := range p {\n\t\t\tif x > maxLit {\n\t\t\t\tw.err = errors.New(\"lzw: input byte too large for the litWidth\")\n\t\t\t\treturn 0, w.err\n\t\t\t}\n\t\t}\n\t}\n\tn = len(p)\n\tcode := w.savedCode\n\tif code == invalidCode {\n\t\t// This is the first write; send a clear code.\n\t\t// https://www.w3.org/Graphics/GIF/spec-gif89a.txt Appendix F\n\t\t// \"Variable-Length-Code LZW Compression\" says that \"Encoders should\n\t\t// output a Clear code as the first code of each image data stream\".\n\t\t//\n\t\t// LZW compression isn't only used by GIF, but it's cheap to follow\n\t\t// that directive unconditionally.\n\t\tclear := uint32(1) << w.litWidth\n\t\tif err := w.write(w, clear); err != nil {\n\t\t\treturn 0, err\n\t\t}","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/compress/lzw/writer.go#L114-L150","documentation":"In lzw.Writer.Write, when litWidth is less than 8 the writer pre-scans every input byte and rejects any byte larger than (1<<litWidth)-1, because the encoder's literal alphabet cannot represent such values. With litWidth == 8 the check is skipped (maxLit becomes 0xFF, all byte values valid). The error is sticky on w.err.","triggerScenarios":"Constructing a Writer with litWidth in [2,7] and then Write-ing a byte whose value exceeds the literal ceiling. E.g., litWidth=2 allows only bytes 0..3; writing byte 0x80 fails immediately.","commonSituations":"GIF encoders that misconfigure litWidth (using 2 for a 256-color image), palette indexing bugs that emit out-of-range indices, or passing litWidth from user input without validation.","solutions":["Use litWidth=8 for general byte data so the full 0..255 alphabet is encodable.","For palette-indexed data, ensure every index is strictly less than (1<<litWidth) and shrink the palette or raise litWidth accordingly.","Validate input bytes before Write rather than letting the encoder fail partway through a large buffer."],"exampleFix":"// before: litWidth=2 cannot encode full palette indices\nw := lzw.NewWriter(dst, lzw.LSB, 2)\nw.Write([]byte{0x00, 0x05, 0xff}) // \"input byte too large\"\n\n// after: choose litWidth that covers the alphabet\nw := lzw.NewWriter(dst, lzw.LSB, 8)\nw.Write([]byte{0x00, 0x05, 0xff})","handlingStrategy":"validation","validationCode":"func maxByteForLitWidth(litWidth int) int {\n    if litWidth >= 8 { return 0xff }\n    return (1 << litWidth) - 1\n}\n\nfunc validateInput(w *lzw.Writer, litWidth int, p []byte) error {\n    max := byte(maxByteForLitWidth(litWidth))\n    if max == 0xff { return nil }\n    for _, x := range p {\n        if x > max { return fmt.Errorf(\"byte %d exceeds litWidth %d alphabet\", x, litWidth) }\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if _, err := w.Write(p); err != nil {\n    if err.Error() == \"lzw: input byte too large for the litWidth\" {\n        // Raise litWidth to 8 or shrink the palette.\n    }\n}","preventionTips":["Use litWidth=8 for general byte data.","For palette indices, assert each index < (1<<litWidth) before Write.","Cross-check GIF's LZW minimum code size against your palette size."],"tags":["lzw","compression","argument-validation","writer","gif"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}