{"record":{"id":"cbc923fa4c4f18eb","repo":"golang/go","slug":"lzw-litwidth-d-out-of-range","errorCode":null,"errorMessage":"lzw: litWidth %d out of range","messagePattern":"lzw: litWidth (.+?) out of range","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/compress/lzw/reader.go","lineNumber":275,"sourceCode":"\nfunc newReader(src io.Reader, order Order, litWidth int) *Reader {\n\tr := new(Reader)\n\tr.init(src, order, litWidth)\n\treturn r\n}\n\nfunc (r *Reader) init(src io.Reader, order Order, litWidth int) {\n\tswitch order {\n\tcase LSB:\n\t\tr.read = (*Reader).readLSB\n\tcase MSB:\n\t\tr.read = (*Reader).readMSB\n\tdefault:\n\t\tr.err = errors.New(\"lzw: unknown order\")\n\t\treturn\n\t}\n\tif litWidth < 2 || 8 < litWidth {\n\t\tr.err = fmt.Errorf(\"lzw: litWidth %d out of range\", litWidth)\n\t\treturn\n\t}\n\n\tbr, ok := src.(io.ByteReader)\n\tif !ok && src != nil {\n\t\tbr = bufio.NewReader(src)\n\t}\n\tr.r = br\n\tr.litWidth = litWidth\n\tr.width = 1 + uint(litWidth)\n\tr.clear = uint16(1) << uint(litWidth)\n\tr.eof, r.hi = r.clear+1, r.clear+1\n\tr.overflow = uint16(1) << r.width\n\tr.last = decoderInvalidCode\n}\n","sourceCodeStart":257,"sourceCodeEnd":291,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/compress/lzw/reader.go#L257-L291","documentation":"`lzw.NewReader` stores the order and `litWidth` (literal-code bit width) on the reader via `init`. `litWidth` must be between 2 and 8 inclusive — values outside that range set `r.err` (note: stored, not returned directly from NewReader) and the reader returns the error on first Read. This width governs the dictionary size; for GIF it is conventionally 8.","triggerScenarios":"Calling `lzw.NewReader(src, order, w)` with w<2 or w>8; treating `litWidth` as a byte count instead of bit width; passing 0 by defaulting from an unset struct field; hardcoding a width tuned for a different LZW variant.","commonSituations":"Decoding GIF/LZW with a misconfigured width pulled from a corrupt header; forking a TIFF reader that supplies a wrong `litWidth`; confusing MSB/LSB AND the width.","solutions":["Pass litWidth in [2,8]; for GIF use 8, for many TIFF readers use 8 unless the image header specifies otherwise.","Validate before constructing: `if litWidth < 2 || litWidth > 8 { return fmt.Errorf(\"bad litWidth %d\", litWidth) }`.","Check the error on the first `Read` from the returned reader, not just from `NewReader` — the constructor defers the error."],"exampleFix":"// before\nr := lzw.NewReader(src, lzw.LSB, 1)\n// after\nr := lzw.NewReader(src, lzw.LSB, 8)","handlingStrategy":"validation","validationCode":"if litWidth < 2 || litWidth > 8 {\n    return fmt.Errorf(\"lzw litWidth must be in [2,8], got %d\", litWidth)\n}","typeGuard":null,"tryCatchPattern":"// the error is deferred to the first Read\nn, err := r.Read(buf)\nif err != nil { /* width or order invalid */ }","preventionTips":["For GIF, use litWidth=8 (or the value from the GIF header).","Check the error on the first Read, not just from NewReader."],"tags":["compress","lzw","argument-validation","gif"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}